Search code examples
pythonubuntusystemdsystemctl

systemd service keep giving me error when start or get status


I have a python application and I need it to be run as a service, I tried many methods and I was advised to make it as systemd service

I searched and tried some code

here is my unit code

[Unit]
Description=Airnotifier Service
After=network.target

[Service]
Type=idle
Restart=on-failure
User=root
ExecStart=python3 /home/airnotifier/airnotifier/app.py

[Install]
WantedBy=multi-user.target

and then I run the following commands

sudo systemctl daemon-reload
sudo systemctl enable airnotifier.service
sudo systemctl start airnotifier.service
sudo systemctl status airnotifier.service

the service does not run and I am getting this errors

airnotifier@airnotifier:~$ sudo systemctl status airnotifier.service
● airnotifier.service - Airnotifier Service
     Loaded: loaded (/lib/systemd/system/airnotifier.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2023-01-09 14:07:38 UTC; 1s ago
    Process: 2072 ExecStart=/usr/bin/python3 /home/airnotifier/airnotifier/app.py (code=exited, status=1/FAILURE)
   Main PID: 2072 (code=exited, status=1/FAILURE)

Jan 09 14:07:38 airnotifier systemd[1]: airnotifier.service: Scheduled restart job, restart counter is at 5.
Jan 09 14:07:38 airnotifier systemd[1]: Stopped Airnotifier Service.
Jan 09 14:07:38 airnotifier systemd[1]: airnotifier.service: Start request repeated too quickly.
Jan 09 14:07:38 airnotifier systemd[1]: airnotifier.service: Failed with result 'exit-code'.
Jan 09 14:07:38 airnotifier systemd[1]: Failed to start Airnotifier Service.

Solution

  • This is the code that works with me

    [Unit]
    Description=Airnotifier Service
    
    [Install]
    WantedBy=multi-user.target
    
    [Service]
    Type=simple
    WorkingDirectory=/home/airnotifier/airnotifier
    ExecStart=python3 app.py
    Restart=always