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
240
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
130
Concurrency Models
9seconds
0
120
Probablistic Data Structures
9seconds
0
200
Own Mustache
9seconds
0
240
Stuff That Works
9seconds
0
220
Evidence
9seconds
0
71
Redneck Monads
9seconds
1
78
Latency
9seconds
0
77
Oh Blindfold Russia!
9seconds
0
210
Other Decks in Programming
See All in Programming
Domain-Driven Transformation
hschwentner
2
1.5k
ServerAction で Progressive Enhancement はどこまで頑張れるか? / progressive-enhancement-with-server-action
takefumiyoshii
6
460
新宿ダンジョンを可視化してみた
satoshi7190
3
400
Ruby GitHub Packages
bkuhlmann
0
650
Going beyond Apache Parquet's default settings
xhochy
0
140
Next.js App Router
quramy
12
2k
Apache Hive 4 on Treasure Data
ryukobayashi
1
440
Git Lint
bkuhlmann
4
760
Ruby Function Composition
bkuhlmann
1
340
初心者のためのRubyKaigi入門/RubyKaigi Introduction
a_matsuda
10
1.6k
Git Rebase
bkuhlmann
11
1.6k
業務ツールとして使うPostman
msys75
0
110
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
290
19k
A Modern Web Designer's Workflow
chriscoyier
689
190k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
34
6k
Happy Clients
brianwarren
92
6.4k
The Cost Of JavaScript in 2023
addyosmani
21
3.9k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
Stop Working from a Prison Cell
hatefulcrawdad
267
19k
Robots, Beer and Maslow
schacon
PRO
155
7.9k
jQuery: Nuts, Bolts and Bling
dougneiner
60
7.2k
Rebuilding a faster, lazier Slack
samanthasiow
74
8.2k
How GitHub (no longer) Works
holman
305
140k
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