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
Sergey Arkhipov
May 20, 2017
Programming
0
300
Daemonize
A little tech talk for rannts#16 meetup on correct daemonization in UNIXes before systed era
Sergey Arkhipov
May 20, 2017
Tweet
Share
More Decks by Sergey Arkhipov
See All by Sergey Arkhipov
Fingerprinting
9seconds
0
150
Concurrency Models
9seconds
0
200
Probablistic Data Structures
9seconds
0
240
Own Mustache
9seconds
0
320
Stuff That Works
9seconds
0
320
Evidence
9seconds
0
86
Redneck Monads
9seconds
1
91
Latency
9seconds
0
110
Oh Blindfold Russia!
9seconds
0
280
Other Decks in Programming
See All in Programming
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
160
High-Level Programming Languages in AI Era -Human Thought and Mind-
hayat01sh1da
PRO
0
410
Create a website using Spatial Web
akkeylab
0
300
GraphRAGの仕組みまるわかり
tosuri13
8
480
Haskell でアルゴリズムを抽象化する / 関数型言語で競技プログラミング
naoya
17
4.9k
「ElixirでIoT!!」のこれまでとこれから
takasehideki
0
370
CursorはMCPを使った方が良いぞ
taigakono
1
180
Beyond Portability: Live Migration for Evolving WebAssembly Workloads
chikuwait
0
390
アンドパッドの Go 勉強会「 gopher 会」とその内容の紹介
andpad
0
260
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
170
AIエージェントはこう育てる - GitHub Copilot Agentとチームの共進化サイクル
koboriakira
0
390
0626 Findy Product Manager LT Night_高田スライド_speaker deck用
mana_takada
0
110
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
A Modern Web Designer's Workflow
chriscoyier
694
190k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
Writing Fast Ruby
sferik
628
61k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
Docker and Python
trallard
44
3.4k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
5
210
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