forked from torfsen/python-systemd-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_demo_service.py
31 lines (24 loc) · 1006 Bytes
/
python_demo_service.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
# Python script for the Python Demo Service
if __name__ == '__main__':
import time
import systemd.daemon
import signal
import sys
# graceful shutdown routine - gets called when process gets SIGTERM (systemd stop) or SIGINT (keyboard ctrl+c)
def graceful_exit(signal_number,stack_frame):
systemd.daemon.notify('STOPPING=1')
print("Python Demo Service received signal {}. Stopping now.".format(signal.Signals(signal_number).name))
time.sleep(5)
print("Python Demo Service has shutdown. Bye bye now!")
sys.exit(0)
print('Starting up ...')
# register SIGTERM and SIGINT handlers to enable graceful shutdown of service
signal.signal(signal.SIGINT, graceful_exit)
signal.signal(signal.SIGTERM, graceful_exit)
time.sleep(10)
print('Startup complete')
# Tell systemd that our service is ready
systemd.daemon.notify('READY=1')
while True:
print('Hello from the Python Demo Service')
time.sleep(5)