Trying to run a service using systemd after startup - python

I have listed below the steps as I entered them. I cannot get the Pi (Raspbian GNU/Linux Buster (10)) to launch alarm.service after startup. When I log into the Pi and launch alarm.service manually from the GUI it works, as does the script it runs -- (The script is written to loop) Only problem is I can't get alarm.service to launch automatically without the GUI. Be great to have it launch last after all other services are up and running.
1. sudo nano /home/pi/alarm_service.py - (fyi: this script runs nicely when i launch it manually from command prompt after pi has booted and i am logged in)
2. sudo nano /etc/systemd/system/alarm.service
[Unit]
Description=Alarm Service
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
Type=simple
ExecStart=/home/pi/alarm_service.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
3. sudo chmod 644 /etc/systemd/system/alarm.service
4. sudo systemctl daemon-reload
5. sudo systemctl enable alarm.service
6. sudo systemctl start alarm.service
Error log:
alarm.service - Alarm service Loaded: loaded (/etc/systemd/system/alarm.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Fri 2021-05-14 18:57:24 AEST; 1min 18s ago Process: 501 ExecStart=/home/pi/alarm_service.py (code=exited, status=1/FAILURE) Main PID: 501 (code=exited, status=1/FAILURE)
May 14 18:57:24 raspberrypi alarm_service.py[501]: File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 10 May 14 18:57:24 raspberrypi alarm_service.py[501]: sock = self._create_socket_connection() May 14 18:57:24 raspberrypi alarm_service.py[501]: File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 35 May 14 18:57:24 raspberrypi alarm_service.py[501]: return socket.create_connection(addr, source_address=source, timeout=sel May 14 18:57:24 raspberrypi alarm_service.py[501]: File "/usr/lib/python2.7/socket.py", line 575, in create_connection May 14 18:57:24 raspberrypi alarm_service.py[501]: raise err May 14 18:57:24 raspberrypi alarm_service.py[501]: socket.error: [Errno 101] Network is unreachable May 14 18:57:24 raspberrypi systemd[1]: alarm.service: Main process exited, code=exited, status=1/FAILURE May 14 18:57:24 raspberrypi systemd[1]: alarm.service: Failed with result 'exit-code'. May 14 18:57:24 raspberrypi systemd[1]: Failed to start Alarm service.

The principle I follow is to make the environment that systemd creates for the script to run as similar to what I use to develop it as possible.
For the sake of testing I just ran shutdown -r now on a VM where I do this exact thing. On boot, voila.
$ systemctl status mypython
● mypython.service
Loaded: ...etc.
The file I have set is
less /etc/systemd/system/multi-user.target.wants/mypython.service
Content
[Unit]
Description=mypython
[Service]
WorkingDirectory=/home/me
ExecStart=/usr/bin/python3 /home/me/mypython.py ./config.yaml
Restart=always
RestartSec=5
User=me
Group=me
[Install]
WantedBy=multi-user.target

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

I have a problem with running a python script as a systemd service

I have a reminder app and i want to run it as a service. I created a service file called 'reminder_py.service' under '/lib/systemd/system'. commands inside 'reminder_py.service':
[Unit]
Description=Dummy Service
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
Type=simple
Username=hrx
Groupname=hrx
ExecStart=/usr/bin/python3 /home/hrx/reminder/reminder.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
And I tried to start it with the following commands:
sudo systemctl daemon-reload
sudo systemctl enable reminder_py.service
sudo systemctl start reminder_py.service
But the sudo systemctl status reminder_py.service command says:
hrx#X230:/lib/systemd/system$ sudo systemctl status reminder_py.service
● reminder_py.service - Dummy Service
Loaded: loaded (/lib/systemd/system/reminder_py.service; enabled; vendor p>
Active: failed (Result: exit-code) since Sat 2020-08-15 23:59:06 +03; 2min>
Process: 13952 ExecStart=/usr/bin/python3 /home/hrx/reminder/reminder.py (c>
Main PID: 13952 (code=exited, status=2)
Aug 15 23:59:06 X230 systemd[1]: Started Dummy Service.
Aug 15 23:59:06 X230 systemd[1]: reminder_py.service: Main process exited, code>
Aug 15 23:59:06 X230 systemd[1]: reminder_py.service: Failed with result 'exit->
How can I solve this problem?
Maybe the following can help:
hrx#X230:/lib/systemd/system$ users
hrx
hrx#X230:/lib/systemd/system$ groups
hrx adm cdrom sudo dip plugdev lpadmin sambashare
hrx#X230:/lib/systemd/system$ ls -al | grep reminder_py.service
-rwxrwxrwx 1 root root 256 Aug 15 23:55 reminder_py.service
And I found a comment from another site that could help me. But I don't quite understand what he's saying.
A fine and simple to follow tutorial. I would add a couple of caveats – with the .service file as written the python script will be run as root and this may have unintended consequences. Also the environment will be different from that for a normal user. To fix add the lines User=username and Group=groupname before the ExecStart line. To add environment variables expected by the script add the line Environment=”variable_name=variable_value” before the ExecStart line.
I solved the problem by replacing contents of the file with:
[Unit]
Description=Reminder App
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
Type=simple
ExecStart=/home/hrx/anaconda3/bin/python /home/hrx/Desktop/reminder/reminder.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
To learn the path of python run this command:
type -a python

Deploying Django project on Centos 8, using Gunicorn, Nginx (Gunicorn.service problem)

I followed this tutorial
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-centos-7
Tried to deploy Django project on centos 8
Everything went fine and worked, except gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=facealatoo
Group=nginx
WorkingDirectory=/home/facealatoo/nadyr/promed
ExecStart=/home/facealatoo/nadyr/promed/venv/bin/gunicorn \
--workers 3 \
--bind unix:/home/facealatoo/nadyr/promed/promed.sock \
configs.wsgi:application
[Install]
WantedBy=multi-user.target
Folders destinations
my project folder destination '/home/facealatoo/nadyr/promed' settings.py file 'home/facealatoo/nadyr/promed/configs/settings.py'
server user name 'facealatoo'
after running
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl status gunicorn.service
Error message
● gunicorn.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor
preset: disabled)
Active: failed (Result: exit-code) since Fri 2020-05-15 18:37:22 +06; 13s
ago
Main PID: 32293 (code=exited, status=203/EXEC)
May 15 18:37:22 facealatoo.net.kg systemd[1]: Started gunicorn daemon.
May 15 18:37:22 facealatoo.net.kg systemd[1]: gunicorn.service: Main
process exited, code=exited, status=203/EXEC
May 15 18:37:22 facealatoo.net.kg systemd[1]: gunicorn.service: Failed
with result 'exit-code'.
Please help me! ;) Thanks in advance ))))
I just change socket file destination (home/facealatoo(user)/) and gunicorn destination (usr/local/bin/gunicorn). And these actions solved my problem)))

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