Python/System Signals: Difference between revisions
< Python
Jump to navigation
Jump to search
(Created page with "<source lang="python3"> import signal def on_ctrl_c(signum, frame): exit(1) signal.signal(signal.SIGINT, on_ctrl_c) </source>") |
(→Daemon) |
||
| (16 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
== Gracefully close a process by signal == | |||
<source lang="python3"> | |||
import signal | |||
def main(): | |||
close_requested = False | |||
def on_close(signum, frame): | |||
nonlocal close_requested | |||
close_requested = True | |||
for signum in (signal.SIGHUP, signal.SIGINT, signal.SIGTERM): | |||
signal.signal(signum, on_close) | |||
while not close_requested: | |||
# TODO | |||
time.sleep(1) | |||
if __name__ == '__main__': | |||
main() | |||
</source> | |||
== Daemon == | |||
'''daemon_test.py''' | |||
<source lang="python3"> | <source lang="python3"> | ||
import os | |||
import signal | import signal | ||
import time | |||
def daemon(): | |||
close_requested = False | |||
def on_quit(signum, frame): | |||
nonlocal close_requested | |||
filepath = os.path.expanduser('~/daemon_test.txt') | |||
with open(filepath, 'a') as f: | |||
msg = 'Daemon(%d) closed by signal %d\n' % (os.getpid(), signum) | |||
f.write(msg) | |||
close_requested = True | |||
focused_signals = [ | |||
signal.SIGHUP, # 1 | |||
signal.SIGINT, # 2 | |||
signal.SIGQUIT, # 3 | |||
signal.SIGABRT, # 6 | |||
signal.SIGTERM # 15 | |||
] | |||
for sig in (focused_signals): | |||
signal.signal(sig, on_quit) | |||
while not close_requested: | |||
print('Daemon running') | |||
time.sleep(1) | |||
if __name__ == '__main__': | |||
if os.fork() == 0: | |||
daemon() | |||
</source> | |||
'''daemon_test.txt ''' | |||
<source lang="bash"> | |||
$ tail -f daemon_test.txt | |||
Daemon(24519) closed by signal 15 | |||
Daemon(24900) closed by signal 1 | |||
Daemon(25621) closed by signal 2 | |||
Daemon(25875) closed by signal 3 | |||
Daemon(27255) closed by signal 6 | |||
</source> | |||
'''Problem on macOS''' | |||
If this daemon ran in a terminal, it would be closed while the terminal was closed.<br/> | |||
To keep the daemon running after the terminal closed, use the following command. | |||
<source lang="python3"> | |||
nohup python3 daemon_test.py > daemon_test.out | |||
</source> | |||
'''Problem on Linux''' | |||
If the original terminal was closed, the stdout would disappear.<br/> | |||
At that time an error occur while print anything.<br/> | |||
Redirect stdout can avoid this problem. | |||
<source lang="python3"> | |||
python3 daemon_test.py > daemon_test.out | |||
</source> | </source> | ||
Latest revision as of 02:43, 12 September 2019
Gracefully close a process by signal
import signal
def main():
close_requested = False
def on_close(signum, frame):
nonlocal close_requested
close_requested = True
for signum in (signal.SIGHUP, signal.SIGINT, signal.SIGTERM):
signal.signal(signum, on_close)
while not close_requested:
# TODO
time.sleep(1)
if __name__ == '__main__':
main()
Daemon
daemon_test.py
import os
import signal
import time
def daemon():
close_requested = False
def on_quit(signum, frame):
nonlocal close_requested
filepath = os.path.expanduser('~/daemon_test.txt')
with open(filepath, 'a') as f:
msg = 'Daemon(%d) closed by signal %d\n' % (os.getpid(), signum)
f.write(msg)
close_requested = True
focused_signals = [
signal.SIGHUP, # 1
signal.SIGINT, # 2
signal.SIGQUIT, # 3
signal.SIGABRT, # 6
signal.SIGTERM # 15
]
for sig in (focused_signals):
signal.signal(sig, on_quit)
while not close_requested:
print('Daemon running')
time.sleep(1)
if __name__ == '__main__':
if os.fork() == 0:
daemon()
daemon_test.txt
$ tail -f daemon_test.txt
Daemon(24519) closed by signal 15
Daemon(24900) closed by signal 1
Daemon(25621) closed by signal 2
Daemon(25875) closed by signal 3
Daemon(27255) closed by signal 6
Problem on macOS
If this daemon ran in a terminal, it would be closed while the terminal was closed.
To keep the daemon running after the terminal closed, use the following command.
nohup python3 daemon_test.py > daemon_test.out
Problem on Linux
If the original terminal was closed, the stdout would disappear.
At that time an error occur while print anything.
Redirect stdout can avoid this problem.
python3 daemon_test.py > daemon_test.out