SystemD python task in Ubuntu 16 - python

I am trying to execute a python3 with systemd in a ubuntu 16.
Following are the configs
[Unit]
Description=email notification server
After=multi-user.target
[Service]
WorkingDirectory=/home/ubuntu/email-noti
Restart=on-failure
ExecStart=/usr/lib/python3 /home/ubuntu/email-noti/email_reader.py
[Install]
WantedBy=multi-user.target
*please note that email_reader.py import a config file and few python3 files from /home/ubuntu/email-noti/
but it always end with following error
● email-noti.service - email notification server
Loaded: loaded (/etc/systemd/system/email-noti.service; enabled; vendor preset: enabled)
Active: inactive (dead) (Result: exit-code) since Tue 2017-03-07 18:00:38 UTC; 6s ago
Process: 11859 ExecStart=/usr/lib/python3 /home/ubuntu/email-noti/email_reader.py (code=exited, status=203/EXEC)
Main PID: 11859 (code=exited, status=203/EXEC)
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: email-noti.service: Service hold-off time over, scheduling restart.
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: Stopped email notification server.
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: email-noti.service: Start request repeated too quickly.
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: Failed to start email notification server.
but when i manually execute the email_reader.py works totally fine.
Any help appreciated

Related

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.
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

Python Nginx Restart and Build

I’m building a web api with Flask. I got error when I use “sudo systemctl restart nginx” for ngnix restart after server installations, wsgi installations
$ sudo systemctl status nginx.servic
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2021-12-06 09:36:31 UTC; 23h ago
Docs: man:nginx(8)
Process: 3210037 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Dec 06 09:36:31 systemd[1]: Starting A high performance web server and a reverse proxy server…
Dec 06 09:36:31 nginx[3210037]: nginx: [emerg] unknown directive “brotli” in /etc/nginx/conf.d/brotli.conf:1
Dec 06 09:36:31 nginx[3210037]: nginx: configuration file /etc/nginx/nginx.conf test failed
Dec 06 09:36:31 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Dec 06 09:36:31 systemd[1]: nginx.service: Failed with result ‘exit-code’.
Dec 06 09:36:31 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
How can I solve this?
I checked sites-enabled and sites-available?

How can i resolve the start limit hit error

Hi I recently bought a Raspberry Pi 4
I am trying to program a discord bot for my server
Here is the error
pi#raspberrypi:~ $ sudo nano /lib/systemd/system/discordbot.service
pi#raspberrypi:~ $ sudo systemctl status discordbot.service
● discordbot.service - My Discord Bot Service
Loaded: loaded (/lib/systemd/system/discordbot.service; enabled; vendor preset: enabled)
Active: failed (Result: start-limit-hit) since Wed 2020-06-03 22:28:41 +08; 20min ago
Main PID: 856 (code=exited, status=0/SUCCESS)
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Service RestartSec=100ms expired, scheduling restart.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Scheduled restart job, restart counter is at 5.
Jun 03 22:28:41 raspberrypi systemd[1]: Stopped My Discord Bot Service.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Start request repeated too quickly.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Failed with result 'start-limit-hit'.
Jun 03 22:28:41 raspberrypi systemd[1]: Failed to start My Discord Bot Service.
Here is the log to see my error
pi#raspberrypi:~ $ journalctl -e -u discordbot
Jun 03 22:28:41 raspberrypi systemd[1]: Started My Discord Bot Service.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Succeeded.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Service RestartSec=1
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Scheduled restart jo
Jun 03 22:28:41 raspberrypi systemd[1]: Stopped My Discord Bot Service.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Start request repeat
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Failed with result '
Jun 03 22:28:41 raspberrypi systemd[1]: Failed to start My Discord Bot Service.
[2]+ Stopped journalctl -e -u discordbot
Here is the code for the systemd
[Unit]
Description=My Discord Bot Service
After=network-online.target
[Service]
Type=idle
Restart=always
User=pi
ExecStart=/usr/bin/python3 /home/pi/discordbot/discordbot.py
[Install]
WantedBy=multi-user.target
I have tried to find solutions for hours but none helped
All help is appreciated :)

Systemd service not recognizing python library

I'm trying to run a python discord bot when my Raspberry Pi starts up. To do this, I've used a systemd service:
[Unit]
Description=Testing
[Service]
Type=idle
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 /home/pi/discord/bug.py
[Install]
WantedBy=multi-user.target
I have done several test using simpler python programs before, and it all works fine. When trying to run the discord bot, it throws an error at the import statement. For testing I run:
sudo systemctl start bugstart
sudo systemctl status bugstart
The output of the status is the following:
bugstart.service - Testing
Loaded: loaded (/lib/systemd/system/bugstart.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2018-08-12 02:08:47 UTC; 1s ago
Process: 1039 ExecStart=/usr/bin/python3 /home/pi/discord/bug.py (code=exited, status=1/FAILURE)
Main PID: 1039 (code=exited, status=1/FAILURE)
Aug 12 02:08:46 raspberrypi systemd[1]: Started Testing.
Aug 12 02:08:47 raspberrypi python3[1039]: Traceback (most recent call last):
Aug 12 02:08:47 raspberrypi python3[1039]: File "/home/pi/discord/bug.py", line 1, in <module>
Aug 12 02:08:47 raspberrypi python3[1039]: import discord
Aug 12 02:08:47 raspberrypi python3[1039]: ImportError: No module named 'discord'
Aug 12 02:08:47 raspberrypi systemd[1]: bugstart.service: Main process exited, code=exited, status=1/FAILURE
Aug 12 02:08:47 raspberrypi systemd[1]: bugstart.service: Unit entered failed state.
Aug 12 02:08:47 raspberrypi systemd[1]: bugstart.service: Failed with result 'exit-code'.
I found out that the commands are run as root, so I figured it might be that the library hand't been installed on root, but I tried importing discord in the command line shell and it worked fine.

Starting bottle web server through systemd?

I am trying to start a bottle web app I wrote using systemd. I made the file /etc/systemd/user/bottle.service with the following contents:
[Unit]
Description=Bottled fax service
After=syslog.target
[Service]
Type=simple
User=fax
Group=fax
WorkingDirectory=/home/fax/bottlefax/
ExecStart=/usr/bin/env python3 server.py
StandardOutput=syslog
StandardError=syslog
Restart=always
RestartSec=2
[Install]
WantedBy=bottle.target
However, when I try to start it, it fails and this is printed in journalctl:
Jun 10 17:33:31 nano systemd[1]: Started Bottled fax service.
Jun 10 17:33:31 nano systemd[1]: Starting Bottled fax service...
Jun 10 17:33:31 nano systemd[2380]: Failed at step GROUP spawning /usr/bin/env: No such process
Jun 10 17:33:31 nano systemd[1]: bottle.service: main process exited, code=exited, status=216/GROUP
Jun 10 17:33:31 nano systemd[1]: Unit bottle.service entered failed state.
Jun 10 17:33:31 nano systemd[1]: bottle.service failed.
How should I fix this?
Edit:
Changing to /usr/bin/python3 as others have suggested results in the same error (changed file though):
Jun 10 18:43:48 nano systemd[1]: Started Bottled fax service.
Jun 10 18:43:48 nano systemd[1]: Starting Bottled fax service...
Jun 10 18:43:48 nano systemd[2579]: Failed at step GROUP spawning /usr/bin/python3: No such process
Jun 10 18:43:48 nano systemd[1]: bottle.service: main process exited, code=exited, status=216/GROUP
Jun 10 18:43:48 nano systemd[1]: Unit bottle.service entered failed state.
Jun 10 18:43:48 nano systemd[1]: bottle.service failed.
I would have commented but I can't :/
Have you tried something like with an absolute path ?
ExecStart=/usr/bin/python3 /path/to/your/server.py
That's the only issue I can see here.
Another possible cause of this error, is if you have created a system user by running adduser --system, and have not created the associated group.

Categories

Resources