Service not running on startup - RabbitMQ used as well - python

I creat a service on raspberry pi, however when rebooting and then checking the services statute I'm getting the below
pi#raspberrypi:~ $ sudo systemctl status non_verbal.service
non_verbal.service - My Sample Service
Loaded: loaded (/lib/systemd/system/non_verbal.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Mon 2022-06-27 12:06:54 BST;
51s ago
Process: 4378 ExecStart=/home/pi/env/bin/python3 /home/pi/linux-sdk-python-
2/examples/audio/classify3.py
Main PID: 4378 (code=exited, status=2)
lines 1-5/5 (END)
Please note that this service should send a message to Rabbitmq which is already running. I have another service as well, which is a receiver to receive messages from the rabbitMQ and perform a certain action.
This is the content of the service file
[Unit]
Description=My Sample Service
After=multi-user.target
[Service]
Type=idle
user=pi
ExecStart=/home/pi/env/bin/python3 /home/pi/linux-sdk-python-2/examples/audio/classify3.py
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target

Related

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

Python script (which open Reverse SSH tunnel) called by service doesn't work

Here is my python script:
#!/usr/bin/env python3
import subprocess
subprocess.run(['ssh', '-fNT','-o', 'ExitOnForwardFailure=yes', '-R', '2222:localhost:22', 'martin#192.168.11.111'])
called by my service:
[Unit]
Description=reverse SSH
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/Public/OnPushButton_PULLUP.py
User=pi
Group=pi
WorkingDirectory=/home/pi/Public/
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
This script exit 0/ Success if I trust systemctl, even if ssh tunnel connection doesn't work after .
● reverse_ssh.service - reverse SSH
Loaded: loaded (/lib/systemd/system/reverse_ssh.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2019-08-01 10:01:21 CEST; 6min ago
Process: 549 ExecStart=/usr/bin/python3 /home/pi/Public/OnPushButton_PULLUP.py (code=exited, status=0/SUCCESS)
Main PID: 549 (code=exited, status=0/SUCCESS)
août 01 10:01:19 raspberrypi systemd[1]: Started reverse SSH.
If I execute this script standalone (I mean like "./script.py") this script works.
At the moment I use service to call it this issue occurs...Where did I do it wrong ??
Thanks !
EDIT
Problem solved. The problem was on my service file.
I had to change"Type:simple" to "Type=forking" because I need to call another process from my python script.
I also have to wait until device get an #IP otherwise the script script trew "Host unreacheable"
For this I used this service file at the end:
[Unit]
Description=reverse SSH
Wants=network-online.target
After=network.target network-online.target
[Service]
Type=forking
ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/python3 /home/pi/Public/OnPushButton_PULLUP.py
User=pi
Group=pi
WorkingDirectory=/home/pi/Public/
TimeoutSec=infinity
[Install]
WantedBy=multi-user.target
Normally adding just this works:
Wants=network-online.target
After=network.target network-online.target
But it didn't for me. That's why I put a :
ExecStartPre=/bin/sleep 10
This line mention to the service that he will wait 10s before trying to be executed. This will give time to device to get #IP from the dhcp.
Finally, forking wasn't the solution. Forking was okay but with this Type of service, the script was stuck on activating until user push the button. This was a problem, other services were waiting for this service to be running, stoped or at least loaded but not stuck on activting. This issue was induced by while loop (true until the user push the button). An then once the user pushed the button the service was running or exit 0, not before.
I changed the service with this following one and it worked:
[Unit]
After=network.target network-online.target
Description=reverse SSH
Wants=network-online.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/OnPushButton_PULLUP.py
ExecStartPre=/bin/sleep 10
Group=pi
RemainAfterExit=yes
TimeoutSec=infinity
Type=simple
User=pi
WorkingDirectory=/home/pi/
[Install]
WantedBy=multi-user.target
Notice the "RemainAfterexit=Yes" otherwise the sshtunnel process(induced by this script) will be closed when the programm is exited.

Trying to configure gunicorn

I have been following this tutorial to deploy my django project on Digital Ocean. I am trying to configure gunicorn.
My project structure looks similar to this:
On my settings.py I use DEBUG=False
I create the gunicorn.socket and gunicorn.service.
/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=someuser
Group=www-data
WorkingDirectory=/home/someuser/myproject
ExecStart=/home/someuser/myproject/myprojectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
Myproject.wsgi:application
[Install]
WantedBy=multi-user.target
I start and enable the Gunicorn socket:
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
Check the status of the process to find out whether it was able to start:
sudo systemctl status gunicorn.socket
This is what I get and then it returns to the command line.
Failed to dump process list, ignoring: No such file or directory
β—� gunicorn.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
Active: active (listening) since Sat 2019-05-04 23:12:03 UTC; 13s ago
Listen: /run/gunicorn.sock (Stream)
CGroup: /system.slice/gunicorn.socket
May 04 23:12:03 myproject systemd[1]: Listening on gunicorn socket.
Next, I check for the existence of the gunicorn.sock file within the /run directory:
file /run/gunicorn.sock
/run/gunicorn.sock: socket
It seems that a file or directory doesn't exist, but it doesn't provide any more details. The gunicorn.sock seems to exist.
I am familiar with Python but not with servers and deploying so I am at a loss here.
I have successfully followed the similar tutorial for Ubuntu 16.04 a few months ago, but now I keep hitting issues.
I know, that's old question, but...
I haved some error. In my case wrong WorkingDirectory and application with modul wsgi. I think that's error in "myproject.wsgi:application"
This is actually an issue with your usage of systemd. You need to refer to the name of the service in your calls to systemctl, not the socket as you're doing. In your case the service will be called gunicorn because you've named your systemd unit file as gunicorn.service.
systemctl enable gunicorn
systemctl start gunicorn

how to start celery flower as as daemon in RHEL?

Im trying to run flower as daemon. My flower.service file read as follows:
[Unit]
Description=Flower Service
After=network.target
[Service]
Type=forking
User=maas
Group=maas
PermissionsStartOnly=true
ExecStart=/bin/flower --broker=amqp://oser000300//
[Install]
WantedBy=multi-user.target
But when i start the service, it is giving error.
//systemctl status flower.service
* flower.service - Flower Service
Loaded: loaded (/etc/systemd/system/flower.service; enabled; vendor preset: disabled)
Active: failed (Result: timeout) since Mon 2017-07-10 20:25:59 UTC; 4min 38s ago
Process: 49255 ExecStart=/bin/flower --broker=amqp://oser000300// (code=exited, status=0/SUCCESS)
Connected to amqp://guest:**#oser000300:5672//
flower.service start operation timed out. Terminating.
SIGTERM detected, shutting down
Failed to start Flower Service.
Unit flower.service entered failed state.
flower.service failed.
I had the same timeout problem when starting the service.
Those parameters has done the trick (i had already celery service running with forking type):
Type=simple
Restart=on-failure

Categories

Resources