2

I am trying to run a python program as a background service in ubuntu 18.04. It imports zerorpc module which I have already installed with pip3 and tested with python command on terminal. But it shows status: failed when I try to run the program as dummy.service. Below is my service file:

[Unit]
Description=Dummy Service
[Service]
Type=Simple
ExecStart=/usr/bin/python3 /usr/bin/server.py
[Install]
WantedBy=multi-user.target

Below is the status of serice after enabling it:

dummy.service - Dummy Service
Loaded: loaded (/lib/systemd/system/dummy.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2019-11-26 16:18:57 IST; 5s ago 

Process: 4101 ExecStart=/usr/bin/python2 /usr/bin/server.py (code=exited, status=1/FAILURE)
Nov 26 16:18:57 vipul-HP-Notebook systemd[1]: Started Dummy Service.

Nov 26 16:18:57 vipul-HP-Notebook python2[4101]: Traceback (most recent call last):

Nov 26 16:18:57 vipul-HP-Notebook python2[4101]:   File "/usr/bin/server.py", line 1, in <module>

Nov 26 16:18:57 vipul-HP-Notebook python2[4101]:     import zerorpc

Nov 26 16:18:57 vipul-HP-Notebook python2[4101]: ImportError: No module named zerorpc

Nov 26 16:18:57 vipul-HP-Notebook systemd[1]: dummy.service: Main process exited, code=exited,      status=1/FAILURE

Nov 26 16:18:57 vipul-HP-Notebook systemd[1]: dummy.service: Failed with result 'exit-code'.

I don't know why this is happening. Please help!!

Vipul Tyagi
  • 547
  • 3
  • 11
  • 29
  • 1
    Your error message does not match your service file (notice what `ExecStart` is). That aside could it be that you have done a pip install of the `zerorpc` package via pip and this is not available when you run it as a service? Maybe you need to ensure all your additional packages are accessible system wide? – Anon Nov 26 '19 at 11:09
  • how would I ensure that all packages are available everywhere? I know little about this. I have also changed python2 to python3. – Vipul Tyagi Nov 26 '19 at 11:16
  • 2
    As already mentioned your ExecStart command is using `python3` but in the logs it shows `python2`. If you have changed the service file you need to reload the systemd manager configuration using `sudo systemctl daemon-reload` – Ionut Ticus Nov 26 '19 at 11:21
  • I have changed service file accordingly but it still shows same error! – Vipul Tyagi Nov 26 '19 at 11:22
  • 1
    Did you run `daemon-reload` after changing the service file? You have to run that command in order for systemd to pickup the new command. Afterwards run `sudo systemctl restart dummy` – Ionut Ticus Nov 26 '19 at 11:28
  • If you were to change to a user other than your own (e.g. `su - otheruser`) can you run `/usr/bin/server.py`? – Anon Nov 26 '19 at 11:31
  • I have corrected it. This time I installed zerorpc with sudo privileges. As Anon said, package should be available system wide. So installing module with sudo, it makes the module available globally. Thank you all! – Vipul Tyagi Nov 26 '19 at 11:31
  • Vipul: I would recommend writing your solution as an answer to this question and then in time you can accept it. – Anon Nov 26 '19 at 11:32
  • Please consider upvoting my question as I'm in dire need of it. – Vipul Tyagi Nov 26 '19 at 11:38
  • Just want to ask one more thing: If my program running as service expects user input, how would I provide one? – Vipul Tyagi Nov 26 '19 at 11:52
  • Vipul: in short - you can't as it's a service and is running unattended. It would be better to alter the program to to read some sort of configuration/data file at start up that contains all it it needs. – Anon Nov 29 '19 at 07:28

1 Answers1

3

If your daemon service is giving error in importing module, the probably you have installed it with pip only. Installing module this way makes it available to a specific user, if you want to make it available globally, you would have to install it with admin privileges like this:

sudo -H pip3 install module_name

Vipul Tyagi
  • 547
  • 3
  • 11
  • 29