Install python package for root user use - python

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.

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"

Cannot run python script as service systemctl Cent

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"

No module named 'gunicorn'

I'm trying to serve Flask with gunicorn and nginx
Here is my systemd unit configuration file
[Unit]
Description=Gunicorn instance to serve odooErp
After=network.target
[Service]
User=tito
Group=www-data
WorkingDirectory=/home/tito/peg/odoo_api/peg_api
Environment="PATH=/home/tito/peg/odoo_api/peg_api/env/bin"
ExecStart=/home/tito/peg/odoo_api/peg_api/env/bin/gunicorn --workers 3 --bind unix:odooErp.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
When I start the service, I run into the following error despite having installed gunicorn using pip
[0;1;31m●[0m odooErp.service - Gunicorn instance to serve productionOdoo
Loaded: loaded (/etc/systemd/system/odooErp.service; enabled; vendor preset: enabled)
Active: [0;1;31mfailed[0m (Result: exit-code) since Tue 2020-08-18 05:33:23 UTC; 1min 0s ago
Main PID: 18305 (code=exited, status=1/FAILURE)
CPU: 43ms
Aug 18 05:33:23 peg-test-01 systemd[1]: Started Gunicorn instance to serve odooErp.
Aug 18 05:33:23 peg-test-01 gunicorn[18305]: Traceback (most recent call last):
Aug 18 05:33:23 peg-test-01 gunicorn[18305]: File "/home/tito/peg/odoo_api/peg_api/env/bin/gunicorn", line 7, in <module>
Aug 18 05:33:23 peg-test-01 gunicorn[18305]: from gunicorn.app.wsgiapp import run
Aug 18 05:33:23 peg-test-01 gunicorn[18305]: ImportError: No module named 'gunicorn'
Aug 18 05:33:23 peg-test-01 systemd[1]: [0;1;39modooErp.service: Main process exited, code=exited, status=1/FAILURE[0m
Aug 18 05:33:23 peg-test-01 systemd[1]: [0;1;39modooErp.service: Unit entered failed state.[0m
Aug 18 05:33:23 peg-test-01 systemd[1]: [0;1;39modooErp.service: Failed with result 'exit-code'.[0m
this is probably because the module is not in correct path
if you are on windows you can set the path as follows in your enviroment :
search this pc > properties > advanced system setting in the left corner > enviromental variable in bottom right corner > in the user varible click PATH then new and set the path
Sometimes there can be some confusion if you are running multiple versions of Python; simply running pip will install a module for one version and not another. I usually prefer running ' -m pip install gunicorn'

ImportError: No module named 'schedule' when i using system to run a service

I have written a script called coinview.py and it can run on linux. When I trying to run it as systemd, it raises error
error:ImportError: No module named 'schedule'.
I use pip3 show schedule, it already exist. So i have no idea what's wrong with my script.
I print sys.executable and sys.path in systemd.
[Unit]
Description=coinview deamon
After=rc-local.service
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/home/ubuntu/source/quotation_api
ExecStart=/usr/bin/python3 coinview.py
Restart=always
[Install]
WantedBy=multi-user.target
ubuntu#ip-100-00-40-02:/etc/systemd/system$ pip3 show schedule
Name: schedule
Version: 0.6.0
Summary: Job scheduling for humans.
Home-page: https://github.com/dbader/schedule
Author: Daniel Bader
Author-email: mail#dbader.org
License: MIT
Location: /home/ubuntu/.local/lib/python3.5/site-packages
Requires:
Required-by:
Mar 27 08:40:10 ip-100-00-40-02 python3[8634]: Traceback (most recent call last):
Mar 27 08:40:10 ip-100-00-40-02 python3[8634]: File "coinview.py", line 3, in <module>
Mar 27 08:40:10 ip-100-00-40-02 python3[8634]: import requests,threading,time,schedule,json
Mar 27 08:40:10 ip-100-00-40-02 python3[8634]: ImportError: No module named 'schedule'
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: coinview.service: Main process exited, code=exited, status=1/FAILURE
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: coinview.service: Unit entered failed state.
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: coinview.service: Failed with result 'exit-code'.
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: coinview.service: Service hold-off time over, scheduling restart.
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: Stopped coinview deamon.
Apr 09 07:59:03 ip-100-00-40-02 python[12095]: /usr/bin/python3
Apr 09 07:59:03 ip-100-00-40-02 python[12095]: ['/home/ubuntu/source/quotation_api', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x8
According to these logs, i found that the PYTHONPATH is different in manual shell and systemd.And i try to add "/home/ubuntu/.local/lib/python3.5/site-packages" into /etc/profile but systemd logs show that it still can't found the path.
So i do a stuip thing, add
sys.path.append("/home/ubuntu/.local/lib/python3.5/site-packages")
in my code, and it works...
Install the package for root with
sudo pip install schedule
Or instead of running it as root, try running it as another specific user.
Modify your .service to something like:
[Unit]
Description=coinview deamon
After=rc-local.service
[Service]
Type=simple
User=user
WorkingDirectory=/home/ubuntu/source/quotation_api
ExecStart=/usr/bin/python3 coinview.py
Restart=always
[Install]
WantedBy=multi-user.target
Hope that helps!

gunicorn status is active (exited) but showing not running in monit

I am trying to setup a new django project on a server from scratch (django+gunicorn+nginx) and I have everything correct except the init scripts for gunicorn.
if I run the gunicorn command manually it works and I can view my site on the ip address, but when I try to do service gunicorn start it gives me this output and it doesn't work...
gunicorn-project.service
Loaded: loaded (/etc/init.d/gunicorn-project; bad; vendor preset: enabled)
Active: active (exited) since Thu 2016-11-17 04:23:56 UTC; 17min ago
Docs: man:systemd-sysv-generator(8)
Process: 1656 ExecStart=/etc/init.d/gunicorn-project start (code=exited, status=0/SUCCESS)
Tasks: 0
Memory: 0B
CPU: 0
Nov 17 04:23:56 project gunicorn-project[1656]: from multiprocessing import cpu_count
Nov 17 04:23:56 project gunicorn-project[1656]: /etc/gunicorn.d/gunicorn-project2.py:3: RuntimeWarning: Parent module '/
Nov 17 04:23:56 project gunicorn-project[1656]: from os import environ
Nov 17 04:23:56 project gunicorn-project[1656]: /etc/gunicorn.d/gunicorn-project3.py:2: RuntimeWarning: Parent module '
Nov 17 04:23:56 project gunicorn-project[1656]: from multiprocessing import cpu_count
Nov 17 04:23:56 project gunicorn-project[1656]: /etc/gunicorn.d/gunicorn-project3.py:3: RuntimeWarning: Parent module '
Nov 17 04:23:56 project gunicorn-project[1656]: from os import environ
Nov 17 04:23:56 project gunicorn-project[1656]: *
Nov 17 04:23:56 project systemd[1]: Started gunicorn-project.service.
Nov 17 04:25:01 project systemd[1]: Started gunicorn-project.service.
I cannot figure out why this is happening... this is the file references in the output...
"""gunicorn WSGI server configuration."""
from multiprocessing import cpu_count
from os import environ
def max_workers():
return cpu_count() * 2 + 1
max_requests = 1000
worker_class = 'gevent'
workers = max_workers()
errorlog = '/home/gunicorn-project/log/gunicorn/error.log'
accesslog = '/home/gunicorn-project/log/gunicorn/access.log'
I just had the very same error message thrown at me (were you also following a Digital Ocean tutorial?) and spent quite a few hours trying to figure it out. I don't know if you still need it, but perhaps I can spare someone else from wasting as much time as I did.
I managed to fix it (after several different attempts) by:
Stopping gunicorn:
sudo systemctl stop gunicorn
Changing writing permissions in both directories containing gunicorn.service:
sudo chmod u+x /etc/systemd/system/multi-user.target.wants/gunicorn-project.service
sudo chmod u+x /etc/systemd/system/gunicorn-project.service
Manually deleting and rebuilding the gunicorn-project.service symlink:
unlink /etc/systemd/system/multi-user.target.wants/gunicorn-project.service
rm /etc/systemd/system/multi-user.target.wants/gunicorn-project.service
ln -s /etc/systemd/system/gunicorn-project.service /etc/systemd/system/multi-user.target.wants/gunicorn-project.service
Reloading gunicorn daemon:
sudo systemctl daemon-reload
Restarting gunicorn:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
And the status changed to active (running). Basically, at some point, I installed gunicorn without setting proper permissions. Once I redid those steps with the right permissions, gunicorn was able to execute as intended.
OBS: my file was named gunicorn.service, which I believe is the default. I changed to gunicorn-project.service to match OP's terminology.

Categories