Search code examples
python-3.7raspbiandaemonsystemd

systemd service activation for Python script fails


I want to register a python script as a daemon service, executed at system startup and running continuously in the background. The script opens network sockets, a local log file and executes a number of threads. The script is well-formed and runs without any compilation or runtime issues.

I used below service file for registration:

[Unit]
Description=ModBus2KNX Gateway Daemon
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/bin/ModBusDaemon.py

[Install]
WantedBy=multi-user.target

Starting the service results in below error:

● ModBusDaemon.service - ModBus2KNX Gateway Daemon
   Loaded: loaded (/lib/systemd/system/ModBusDaemon.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2021-01-04 21:46:29 CET; 6min ago
  Process: 1390 ExecStart=/usr/bin/python3 /usr/bin/ModBusDaemon.py (code=exited, status=1/FAILURE)
 Main PID: 1390 (code=exited, status=1/FAILURE)

Jan 04 21:46:29 raspberrypi systemd[1]: Started ModBus2KNX Gateway Daemon.
Jan 04 21:46:29 raspberrypi systemd[1]: ModBusDaemon.service: Main process exited, code=exited, status=1/FAILURE
Jan 04 21:46:29 raspberrypi systemd[1]: ModBusDaemon.service: Failed with result 'exit-code'.

Appreciate your support!


Solution

  • Related posts brought me to the resolution for my issue. Ubuntu systemd custom service failing with python script refers to the same issue. The proposed solution adding the WorkingDirectory to the Service section resolved the issue for me. Though, I could not find the adequate systemd documentation outlining on the implicit dependency.