Search code examples
pythonsystemd

Python script does not restart properly with systemd


I am learning how to restart a Python script in case of error (following this tutorial, however with some small tweaks regarding filenames and alike). First things first, the needed files:

  • /home/myuser/Desktop/test/test.py:
from datetime import datetime
from time import sleep

path = "/home/dec13666/Desktop/test/log.txt"

while True:
    with open(path, "a") as f:
        now = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
        f.write(now+"\n")
        f.close()
    sleep(1)
    raise Exception("Error Simulation!!!")
  • davidcustom.service:
[Unit]
Description=Python Script Made By Me
After=multi-user.target

[Service]
RestartSec=10
Restart=always
ExecStart=python3 /home/myuser/Desktop/test/test.py

[Install]
WantedBy=multi-user.target

Finally, the commands run:

sudo nano /etc/systemd/system/davidcustom.service
sudo systemctl daemon-reload
sudo systemctl enable davidcustom.service
sudo systemctl start davidcustom.service
sudo systemctl status davidcustom.service

The message I am getting:

● davidcustom.service - Python Script Made By Me
     Loaded: loaded (/etc/systemd/system/davidcustom.service; enabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Sun 2022-09-04 14:12:12 EDT; 8s ago
    Process: 3341 ExecStart=python3 /home/myuser/Desktop/test/test.py (code=exited, status=1/FAILURE)
   Main PID: 3341 (code=exited, status=1/FAILURE)
        CPU: 100ms

Notes:

  • When I run test.py manually, it works OK, but then that python script is run from a service (as seen here), it generates that error.
  • I have tried to set User=myusername and Type=simple, in davidcustom.service ([Service]), with no difference in the results.

What am I doing wrong?


Solution

  • I inadvertently added some wrong indentation in the original code (corrected by now in the original post). That solved my issue; when running sudo systemctl status davidcustom.service, now the service is active:

    ● davidcustom.service - Python Script Made By Dave
         Loaded: loaded (/etc/systemd/system/davidcustom.service; enabled; vendor preset: enabled)
         Active: active (running) since Sun 2022-09-04 16:16:02 EDT; 23ms ago
       Main PID: 4287 (python3)
          Tasks: 1 (limit: 14215)
         Memory: 3.0M
            CPU: 16ms
         CGroup: /system.slice/davidcustom.service
                 └─4287 python3 /home/dec13666/Desktop/test/test.py
    

    Other suggestions which would worth keeping in mind, but were NOT necessary for my case (thanks @Barmar & @tdelaney):

    • Use an absolute path at ExecStart (easily obtainable by running which python command)
    • If you only want to use your User and/or Group, then explicitely write those parameters.
    • The 2 previous suggestions should be added in [Service], in your service script:
    [Service]
    RestartSec=10
    Restart=always
    ExecStart=/usr/bin/python3 /home/myuser/Desktop/test/test.py
    User=youruser
    Group=yourgroup
    
    • Read the comments in the original post, for other options.

    Thanks.