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
370
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
300
Probablistic Data Structures
9seconds
0
290
Own Mustache
9seconds
0
370
Stuff That Works
9seconds
0
400
Evidence
9seconds
0
120
Redneck Monads
9seconds
1
140
Latency
9seconds
0
170
Oh Blindfold Russia!
9seconds
0
340
Other Decks in Programming
See All in Programming
Go を使い始めて 2 ヶ月の学び / My first two months with Go
contour_gara
0
410
Deep dive into the select statement (GopherCon UK)
jespino
0
170
XHTMLが残したもの
yosuke_furukawa
PRO
1
380
Oxlintはいいぞ(続)
yug1224
1
530
「つくるAI」だけではバグは見つからない ~テストに必要な「見つけるAI」を分離させる戦略~
mfunaki
0
230
ALB ログから Trace を気合で繋げる技術
fohte
7
860
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
0
130
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
180
Gmail/Google DriveをトリガーにAIエージェントを動かそう! / Run AI agents with Gmail/Google Drive as triggers!
har1101
3
460
[PyCon KR 2026] More Variants, More Diversity for AI Accelerators
achimnol
0
130
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
1
430
이 함수, 실패하면 어떻게 되나요? null부터 Rich Errors까지, Kotlin 에러 처리 15년
haeti2
0
120
Featured
See All Featured
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
690
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
Docker and Python
trallard
47
4.2k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.7k
WCS-LA-2024
lcolladotor
0
820
How to build a perfect <img>
jonoalderson
1
5.9k
The browser strikes back
jonoalderson
0
1.6k
RailsConf 2023
tenderlove
30
1.5k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
360
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Navigating Weather and Climate Data
rabernat
0
510
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
180
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