Marc
Marc

Reputation: 3313

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?

Upvotes: 1

Views: 3274

Answers (1)

Marc
Marc

Reputation: 3313

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.

Upvotes: 1

Related Questions