-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsignaller.py
executable file
·57 lines (48 loc) · 1.6 KB
/
signaller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import sys
import os
import time
import signal
import atexit
signaller_log = open("signaller.log", "w")
fatal_sigs = [signal.SIGINT, signal.SIGTERM, signal.SIGQUIT, signal.SIGHUP, signal.SIGPIPE, signal.SIGABRT]
continue_on_sigs=[]
def print_msg(msg):
for f in (sys.stderr, signaller_log):
f.write('signaller: ' + msg + "\n")
f.flush()
def handler(signum, frame):
signame = signal.Signals(signum).name
print_msg(f'signal handler called with signal {signame} ({signum}) at {time.time()}')
sys.stderr.flush()
signaller_log.flush()
if signum not in continue_on_sigs:
print_msg(f'exiting on {signal.Signals(signum).name}')
sys.exit(1)
def remove_pidfile_atexit():
try:
os.unlink("signaller.pid")
except Exception as exc:
print_msg(f"unlinking pidfile: {exc}")
def main(args):
global continue_on_sigs
continue_args_list = args[1:]
if not continue_args_list:
# by default, mask SIGINT
continue_args_list = ['SIGINT']
continue_on_sigs = [ signal.Signals[signame.upper()] for signame in continue_args_list if signame != "" ]
print_msg(f"continue on sigs: {continue_on_sigs}")
for sig in fatal_sigs:
signal.signal(sig, handler)
print_msg(f"my pid is {os.getpid()}")
with open("signaller.pid", "w") as pidfile:
pidfile.write(f"{os.getpid()}\n")
pidfile.flush()
atexit.register(remove_pidfile_atexit)
while True:
print_msg(f"tick {time.time()}")
sys.stderr.flush()
signaller_log.flush()
time.sleep(1);
if __name__ == '__main__':
main(sys.argv)