meta data for this page
  •  

daemon

systemd

Python daemon and systemd service

When using systemd, it is no need to implement daemon in main process. Systemd simply works with foreground services (default service type=simple. See Type=).

Hints:

  • To force systemd to wait for full process startup, use sd_notify(3) and set unit type=notify
  • Notify also ensure that ExecStartPost will be executed after notify received.
  • To prevent output buffering and correct journal logging set PYTHONUNBUFFERED=1 environment or flush stdout more often sys.stdout.flush()

Example:

import systemd.daemon
 
# long startup
# ...
# long startup done
systemd.daemon.notify('READY=1')
 
# start servicing
[Service]
Type=notify
ExecStart=/usr/bin/my_service
ExecStartPost=/usr/bin/my_service_client
Environment=PYTHONUNBUFFERED=1