Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Daemonize
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Sergey Arkhipov
May 20, 2017
Programming
360
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Daemonize
A little tech talk for rannts#16 meetup on correct daemonization in UNIXes before systed era
Sergey Arkhipov
May 20, 2017
More Decks by Sergey Arkhipov
See All by Sergey Arkhipov
Fingerprinting
9seconds
0
190
Concurrency Models
9seconds
0
290
Probablistic Data Structures
9seconds
0
280
Own Mustache
9seconds
0
360
Stuff That Works
9seconds
0
390
Evidence
9seconds
0
110
Redneck Monads
9seconds
1
130
Latency
9seconds
0
160
Oh Blindfold Russia!
9seconds
0
320
Other Decks in Programming
See All in Programming
肥大化するレガシーコードに立ち向かうためのインターフェース分離と依存の逆転 / JJUG CCC 2026 Spring
hirokunimaeta
0
560
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
140
3Dシーンの圧縮
fadis
1
770
タクシーアプリ『GO』の バックエンド開発のおける AI利活用と若者のすべて
pyama86
3
2k
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
190
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
790
JavaDoc 再入門
nagise
1
350
RTSPクライアントを自作してみた話
simotin13
0
610
dRuby over BLE
makicamel
2
340
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
160
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
340
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
280
Featured
See All Featured
Accessibility Awareness
sabderemane
1
140
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
140
Information Architects: The Missing Link in Design Systems
soysaucechin
0
970
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Context Engineering - Making Every Token Count
addyosmani
9
970
4 Signs Your Business is Dying
shpigford
187
22k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
ラッコキーワード サービス紹介資料
rakko
1
3.7M
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
Everyday Curiosity
cassininazir
0
230
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Transcript
Правильная демонизация Сергей Архипов, 2017
Чего хотим #!/usr/bin/env python3 # -*- coding: utf-8 -*- import
contextlib import syslog import time @contextlib.contextmanager def daemonize(): yield if __name__ == "__main__": with daemonize(): syslog.openlog(facility=syslog.LOG_USER) syslog.syslog("Daemon start.") time.sleep(10) syslog.syslog("Daemon done.")
Подход 1 def daemonize(): first_pid = os.fork() if first_pid: sys.exit(0)
yield
Подход 2 def daemonize(): first_pid = os.fork() if first_pid: sys.exit(0)
syslog.syslog("Session={0}, Group={1}, Parent={2}, Self={3}".format( os.getsid(0), os.getpgid(0), os.getppid(), os.getpid() )) yield … Session=7087, Group=14115, Parent=14115, Self=14151 … Daemon start. … Daemon done.
Подход 2 def daemonize(): first_pid = os.fork() if first_pid: sys.exit(0)
syslog.syslog("Session={0}, Group={1}, Parent={2}, Self={3}".format( os.getsid(0), os.getpgid(0), os.getppid(), os.getpid() )) os.setsid() syslog.syslog("Session={0}, Group={1}, Parent={2}, Self={3}".format( os.getsid(0), os.getpgid(0), os.getppid(), os.getpid() )) yield … Session=7087, Group=17364, Parent=17364, Self=17400 … Session=17400, Group=17400, Parent=17364, Self=17400 … Daemon start … Daemon done.
Подход 3 def daemonize(): if os.fork(): sys.exit(0) os.setsid() if os.fork():
sys.exit(0) yield
Подход 4 def daemonize(): if os.fork(): sys.exit(0) os.setsid() if os.fork():
sys.exit(0) os.chdir("/") os.umask(0o002) yield
Подход 5 def daemonize(): if os.fork(): sys.exit(0) os.setsid() if os.fork():
sys.exit(0) os.chdir("/") os.umask(0o002) sys.stdout.flush() sys.stderr.flush() _, max_fds = resource.getrlimit(resource.RLIMIT_NOFILE) max_fds = max_fds if max_fds!=resource.RLIM_INFINITY else 1024 os.closerange(0, max_fds) sys.stdout = sys.stderr = open(os.devnull, "w") sys.stdin = open(os.devnull, "r") yield
Подход 6 def daemonize(): if os.getppid() != 1: return if
os.fork(): sys.exit(0) os.setsid() if os.fork(): sys.exit(0) os.chdir("/") os.umask(0o002) sys.stdout.flush() sys.stderr.flush() _, max_fds = resource.getrlimit(resource.RLIMIT_NOFILE) max_fds = max_fds if max_fds != resource.RLIM_INFINITY else 1024 os.closerange(0, max_fds) sys.stdout = sys.stderr = open(os.devnull, "w") sys.stdin = open(os.devnull, "r") yield
def daemonize(): if os.getppid() != 1: return if os.fork(): sys.exit(0)
os.setsid() if os.fork(): sys.exit(0) os.chdir("/") os.umask(0o002) sys.stdout.flush() sys.stderr.flush() _, max_fds = resource.getrlimit(resource.RLIMIT_NOFILE) max_fds = max_fds if max_fds != resource.RLIM_INFINITY else 1024 os.closerange(0, max_fds) sys.stdout = sys.stderr = open(os.devnull, "w") sys.stdin = open(os.devnull, "r") yield