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
270
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
140
Concurrency Models
9seconds
0
180
Probablistic Data Structures
9seconds
0
220
Own Mustache
9seconds
0
300
Stuff That Works
9seconds
0
280
Evidence
9seconds
0
77
Redneck Monads
9seconds
1
85
Latency
9seconds
0
98
Oh Blindfold Russia!
9seconds
0
260
Other Decks in Programming
See All in Programming
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
230
バックエンドのためのアプリ内課金入門 (サブスク編)
qnighy
8
1.8k
2024年のkintone API振り返りと2025年 / kintone API look back in 2024
tasshi
0
220
データベースのオペレーターであるCloudNativePGがStatefulSetを使わない理由に迫る
nnaka2992
0
170
Open source software: how to live long and go far
gaelvaroquaux
0
640
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
920
Domain-Driven Transformation
hschwentner
2
1.9k
GitHub Actions × RAGでコードレビューの検証の結果
sho_000
0
270
Djangoにおける複数ユーザー種別認証の設計アプローチ@DjangoCongress JP 2025
delhi09
PRO
4
210
color-scheme: light dark; を完全に理解する
uhyo
5
420
時計仕掛けのCompose
mkeeda
1
300
sappoRo.R #12 初心者セッション
kosugitti
0
260
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.3k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.2k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Navigating Team Friction
lara
183
15k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
Mobile First: as difficult as doing things right
swwweet
223
9.3k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Bootstrapping a Software Product
garrettdimon
PRO
306
110k
What's in a price? How to price your products and services
michaelherold
244
12k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
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