Cannot run python script as service systemctl Cent - python

I have following systemctl service
[Unit]
Description=Ml api
[Service]
#user=root
ExecStart=/usr/local/bin/python3.9 -u /home/a.nikitin#corp.bsv.legal/bsv_ml_api/app.py
ExecStop=/bin/kill -15 $MAINPID
Type=forking
#KillMode=process
#SyslogIdentifier=ml-api
#SyslogFacility=daemon
Restart=on-failure
[Install]
WantedBy=multiuser.target
When i run it i got an error. The sudo journalctl -u ml.service -e shows
ml.service - Ml api
Loaded: loaded (/usr/lib/systemd/system/ml.service; disabled; vendor preset: disabled)
Active: failed (Result: start-limit) since Fri 2022-06-10 11:38:38 MSK; 1 day 23h ago
Main PID: 77614 (code=exited, status=203/EXEC)
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: Unit ml.service entered failed state.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: ml.service failed.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: ml.service holdoff time over, scheduling restart.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: Stopped Ml api.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: start request repeated too quickly for ml.service
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: Failed to start Ml api.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: Unit ml.service entered failed state.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: ml.service failed.
However, if i run directly
/usr/local/bin/python3.9 -u /home/a.nikitin#corp.bsv.legal/bsv_ml_api/app.py
Everything is ok and the script started
INFO: Started server process [4401]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:1111 (Press CTRL+C to quit)
I don't know where the problem is. It's FastAPI + uvicorn app.

Turns out i need to specify python env
Environment="PYTHONPATH=$PYTHONPATH:/home/a.nikitin#corp.bsv.legal/.local/lib/python3.9/site-packages"

Related

How do I debug a exit-code failure for the specific service for my app?

I'm trying to run a Flask app through Apache. When I run
systemctl list-units --type=service
I see that the unit for this specific app, "SITENAME.service" has failed.
When I run
sudo systemctl status SITENAME.service
I
get an error saying:
Loaded: loaded (/etc/systemd/system/SITENAMEenv.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2023-01-01 21:26:25 UTC; 1min 1s ago
Process: 787 ExecStart=/home/ubuntu/SITENAME/SITENAMEenv/bin/uwsgi --ini SITENAMEenv.ini (code=exited, status=1/FAILURE)
Main PID: 787 (code=exited, status=1/FAILURE) Jan 01 21:26:25 ip-172-31-88-10 systemd[1]:
SITENAME.service: Main process exited, code=exited, status=1/FAILURE Jan 01 21:26:25 ip-172-31-88-10 systemd[1]: SITENAME.service: Failed with result 'exit-code'.
Where can I get more information on this failure? I can't tell if this is a python issue in the actual application code, or something else.
The site stopped working a little while ago, but there were no major changes to the code. The service that is not working has the description "uWSGI instance to serve SITENAME"

How to figure out why systemd service failed with result 'exit-code'?

I've created a python script that collects info for a 5 minute period and then sends data to aws cloudwatch. The body of the script runs forever in a while True: block, and when I run it on its own using python3 to_cloud.py, it functions successfully.
I made a service for it as follows:
[Unit]
Description=Service for sending script data to cloudwatch
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/ubuntu/scripts/to_cloud.py
WorkingDirectory= /home/ubuntu/scripts
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
When I start the service, it runs for the duration of the internal loop that collects the info, but then the following happens:
● to_cloud.service - Service for sending script data to cloudwatch
Loaded: loaded (/lib/systemd/system/to_cloud.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2021-06-23 16:53:44 UTC; 5s ago
Process: 191072 ExecStart=/usr/bin/python3 /home/ubuntu/scripts/to_cloud.py (code=exited, status=1/FAILURE)
Main PID: 191072 (code=exited, status=1/FAILURE)
Jun 23 16:52:43 ip-172-31-19-11 systemd[1]: Started Service for sending script data to cloudwatch.
Jun 23 16:53:44 ip-172-31-19-11 systemd[1]: to_cloud.service: Main process exited, code=exited, status=1/FAILURE
Jun 23 16:53:44 ip-172-31-19-11 systemd[1]: to_cloud.service: Failed with result 'exit-code'.
There are no logs in the journalctl so I'm wondering how to figure out what's going wrong. Thanks!

Can't deploy telegram bot on ubuntu server

HI i've made telegram bot and trying to deploy it on Ubuntu 18.04
Requirements:
firebase==3.0.1
firebase-admin==4.3.0
pyTelegramBotAPI==3.7.1
grpcio==1.29.0
requests==2.23.0
I installed this on server:
build-essential
libssl-dev
libffi-dev
python3-pip
python3-dev
python3-setuptools
python3-venv
After creating venv, i go to /etc/systemd/system/ and made uba.service
uba.service
[Unit]
Description=uba
After=network.target
[Service]
User=uba
Group=uba
WorkingDirectory=/home/kokoto/uba/
Environment="PYTHONPATH=/home/kokoto/uba/"
ExecStart=/home/kokoto/uba/.venv/bin/python /home/kokoto/uba/bot_bd.py
[Install]
WantedBy=multi-user.target
But when i'm running bot, i have this errors:
uba.service - uba
Loaded: loaded (/etc/systemd/system/uba.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-09-21 17:48:05 MSK; 16h ago
Process: 10282 ExecStart=/home/kokoto/uba/.venv/bin/python /home/kokoto/uba/bot_bd.py (code=exited, status=216/GROUP)
Main PID: 10282 (code=exited, status=216/GROUP)
Sep 21 17:48:05 s307229 systemd[1]: Started uba.
Sep 21 17:48:05 s307229 systemd[10282]: uba.service: Failed to determine group credentials: No such process
Sep 21 17:48:05 s307229 systemd[10282]: uba.service: Failed at step GROUP spawning /home/kokoto/uba/.venv/bin/python: No such process
Sep 21 17:48:05 s307229 systemd[1]: uba.service: Main process exited, code=exited, status=216/GROUP
Sep 21 17:48:05 s307229 systemd[1]: uba.service: Failed with result 'exit-code'.
I am not sure, but may be ubuntu uses python2 version. Try to change python to python3
ExecStart=/home/kokoto/uba/.venv/bin/python3 /home/kokoto/uba/bot_bd.py

Install python package for root user use

I've read all over, and I still can't get my python script to run in systemd.
Here is the shell script I use:
#! /bin/sh
cd /home/albert/speedcomplainer
/usr/bin/python speedcomplainer.py
I can execute the script (/usr/bin/speedcomplainer), it runs just fine from the command line. The python script loops forever, checking my internet speeds. As I said, it runs fine, from the command line directly (python ...) or from the shell script I created in usr/bin.
But when I put it into this unit file:
# speedcomplianer - checks and tweets comcast speeds.
#
#
[Unit]
Description=Ethernet Speed Complainer
After=syslog.target network.target
[Service]
Type=simple
WorkingDirectory=/home/albert/speedcomplainer
ExecStart=/usr/bin/speedcomplainer
Restart=always
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target
It fails to startup (sudo systemctl start speedcomplainer.service) with this error:
speedcomplainer.service - Ethernet Speed Complainer
Loaded: loaded (/lib/systemd/system/speedcomplainer.service; enabled; vendor preset: enabled)
Active: failed (Result: start-limit) since Wed 2016-02-24 20:21:02 CST; 7s ago
Process: 25325 ExecStart=/usr/bin/speedcomplainer (code=exited, status=1/FAILURE)
Main PID: 25325 (code=exited, status=1/FAILURE)
I look at the log with journalctl -u speedcomplainer and :
Feb 24 20:21:02 haven systemd[1]: Started Ethernet Speed Complainer.
Feb 24 20:21:02 haven speedcomplainer[25325]: Traceback (most recent call last):
Feb 24 20:21:02 haven speedcomplainer[25325]: File "speedcomplainer.py", line 9, in <module>
Feb 24 20:21:02 haven speedcomplainer[25325]: import twitter
Feb 24 20:21:02 haven speedcomplainer[25325]: ImportError: No module named twitter
Feb 24 20:21:02 haven systemd[1]: speedcomplainer.service: Main process exited, code=exited, status=1/FAILURE
Feb 24 20:21:02 haven systemd[1]: speedcomplainer.service: Unit entered failed state.
Feb 24 20:21:02 haven systemd[1]: speedcomplainer.service: Failed with result 'exit-code'.
Feb 24 20:21:02 haven systemd[1]: speedcomplainer.service: Service hold-off time over, scheduling restart.
Feb 24 20:21:02 haven systemd[1]: Stopped Ethernet Speed Complainer
AHAHA!! An import error in the python script.
But wait - it works from everywhere else. Why am I getting an Import error only when it runs from systemd? (Answer - the module is installed locally. Next question:)
OK. After following the path that #jcomeau_ictx lead me down, it seems that pip installed to my local user directory. How do I install modules for root use?
OK. Thanks to jcomeau_ictx, I figured out the problem. pip installs locally, by default. This post discussed in detail how to install systemwide (TL;DR apt-get.). This installed for the root user. I didn't want to mess with a virtual env, and it's only one module with few dependencies.

python app (daemon) and systemd fedora

I have python app with loop, which generates some files, saves video images and some other stuff. i installed it on Fedora (17) PC and want it to run "forever", ie if it hangs (i can put some keep_alive in file in loop) - it should be restarted. It also should be started on reboot.
As i understand, python-daemon help to do this, and systemd in Fedora.
I have the following config file for systemd (im not sure on some parameters though as documentation is too complicated for my level of linux knowledge):
[Unit]
Description=TPR Daemon
[Service]
Type=forking
Restart=always
WorkingDirectory=/home/igor/tpr
PIDFile=/var/run/tpr.pid
ExecStart=/usr/bin/python /home/igor/tpr/testd.py
[Install]
WantedBy=default.target
and here is my testd.py:
import daemon
import time, sys
class MyDaemon(object):
def __init__(self):
pass
def run(self):
while True:
print 'I am alive!'
time.sleep(1)
if __name__ == '__main__':
with daemon.DaemonContext(stdout=sys.stdout):
check = MyDaemon()
check.run()
when i run it with "sudo systemctl start tpr.service", it hangs for a while and then cancels out with this meesage:
Warning: Unit file of tpr.service changed on disk, 'systemctl --system daemon-reload' recommended.
Job for tpr.service failed. See 'systemctl status tpr.service' and 'journalctl -xn' for details.
and here are some logs from /var/log/messages:
Aug 9 21:32:27 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 9 21:32:27 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 9 21:32:27 localhost systemd[1]: Stopping TPR Daemon...
Aug 9 21:32:27 localhost systemd[1]: Starting TPR Daemon...
Aug 9 21:33:57 localhost systemd[1]: tpr.service operation timed out. Terminating.
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
...
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost systemd[1]: tpr.service: control process exited, code=exited status=1
Aug 9 21:33:57 localhost systemd[1]: Failed to start TPR Daemon.
Aug 9 21:33:57 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 9 21:33:57 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 9 21:33:57 localhost systemd[1]: Stopping TPR Daemon...
Aug 9 21:33:57 localhost systemd[1]: Starting TPR Daemon...
so it should be running, but what this error about?
And maybe there is some simple convenient way to accomplish my task, and im inventing bicycle?
Update:
It seems the daemon should somehow let systemd know it has started..but how?
Aug 10 01:15:36 localhost systemd[1]: Starting TPR Daemon...
Aug 10 01:17:06 localhost systemd[1]: tpr.service operation timed out. Terminating.
Aug 10 01:17:06 localhost systemd[1]: tpr.service: control process exited, code=exited status=1
Aug 10 01:17:06 localhost systemd[1]: Failed to start TPR Daemon.
Aug 10 01:17:06 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 10 01:17:06 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 10 01:17:06 localhost systemd[1]: Stopping TPR Daemon...
Aug 10 01:17:06 localhost systemd[1]: Starting TPR Daemon...
Aug 10 01:18:36 localhost systemd[1]: tpr.service operation timed out. Terminating.
Aug 10 01:18:36 localhost systemd[1]: tpr.service: control process exited, code=exited status=1
Aug 10 01:18:36 localhost systemd[1]: Failed to start TPR Daemon.
Aug 10 01:18:36 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 10 01:18:36 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 10 01:18:36 localhost systemd[1]: Stopping TPR Daemon...
Aug 10 01:18:36 localhost systemd[1]: Starting TPR Daemon...
The error about changing on the disk means just that the file is changed.
After you run systemctl daemon-reload the file will be reread and hten you'll be able to start the service.
You can use notifications as said in this manual pages. The type for the service is notify.
Next thing is: you say your service type as forking. Does your process really forks? It is recommended to have PIDfile option set up if you use forking.
With systemd it is not necessary to fork your process to become daemon.

Categories