-1

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.
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175
  • This question is already answered over here. https://unix.stackexchange.com/questions/697995/systemctl-service-failed-exit-code – Shantanu Srivastava Jan 09 '23 at 14:19
  • @ShantanuSrivastava it is not the same problem – Amira Elsayed Ismail Jan 09 '23 at 15:16
  • From the tag: systemd questions should be for *programming questions* using systemd or its libraries. Questions about *configuring the daemon* (including writing unit files) are better directed to Unix & Linux: https://unix.stackexchange.com. Please delete this. – Rob Jan 10 '23 at 12:12

1 Answers1

0

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
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175