Python/System Signals: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 75: Line 75:


If this daemon ran in a terminal, it would be closed while the terminal was closed.<br/>
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, the following command is necessary.
To keep the daemon running after the terminal closed, use the following command.


<source lang="python3">
<source lang="python3">
Line 82: Line 82:


'''Problem on Linux'''
'''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>

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