0

I just got everything set and done with my python flask project and I would like to make a service out of it.

In order to get started, I've made a bash file which is supposed to start the server and can be located in /home/pi

#!/bin/bash
cd /home/pi/Desktop/secure-pi-tensorflow
python3 /home/pi/Desktop/secure-pi-tensorflow/run.py

My service file looks like this:

[Unit]
After=multi-user.target

[Service]
Type=idle
ExecStart=/home/pi/securepi.sh

[Install]
WantedBy=multi-user.target

The problem that I am facing is that I am able to power up the server with the bash file, but it doesn't start as a service, this error pops up:

pi@securepi:~ $ systemctl status securepi.service
Warning: The unit file, source configuration file or drop-ins of securepi.service changed on disk. Run 'systemctl daemon-reload' to reload units.
● securepi.service
   Loaded: loaded (/lib/systemd/system/securepi.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2019-10-28 15:42:39 GMT; 8min ago
  Process: 1325 ExecStart=/usr/bin/python3 /home/pi/Desktop/secure-pi-tensorflow/run.py (code=exited, status=1/FAILURE)
 Main PID: 1325 (code=exited, status=1/FAILURE)

Oct 28 15:42:38 securepi systemd[1]: Started securepi.service.
Oct 28 15:42:39 securepi python3[1325]: Traceback (most recent call last):
Oct 28 15:42:39 securepi python3[1325]:   File "/home/pi/Desktop/secure-pi-tensorflow/run.py", line 12, in <module>
Oct 28 15:42:39 securepi python3[1325]:     from securepi import app
Oct 28 15:42:39 securepi python3[1325]:   File "/home/pi/Desktop/secure-pi-tensorflow/securepi/__init__.py", line 4, in <module>
Oct 28 15:42:39 securepi python3[1325]:     from flask_jsglue import JSGlue
Oct 28 15:42:39 securepi python3[1325]: ModuleNotFoundError: No module named 'flask_jsglue'
Oct 28 15:42:39 securepi systemd[1]: securepi.service: Main process exited, code=exited, status=1/FAILURE
Oct 28 15:42:39 securepi systemd[1]: securepi.service: Failed with result 'exit-code'.

Does anybody have any idea what's going on with that error? Everything runs just fine when powered by the bash file but always fails when the service tries

Also I would like to mention that the flask_jsglue in installed for both python2.7 and 3

pi@securepi:~ $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask_jsglue
[2]+  Stopped                 python3
pi@securepi:~ $ python
Python 2.7.16 (default, Apr  6 2019, 01:42:57) 
[GCC 8.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask_jsglue
9
  • How did you install flask_jsglue ? Via pip or with your package manager?
    – schaiba
    Oct 28 '19 at 15:55
  • I used pip3 to install it Oct 28 '19 at 15:55
  • maybe it's the user that's running it?
    – rogerdpack
    Oct 28 '19 at 16:00
  • how could I check that? Oct 28 '19 at 16:01
  • 1
    all right so I found out that If I add User=pi in service it works just fine, but how could I possibly make this global for all users? Oct 28 '19 at 16:16
1

You just have to add your system user (pi or ubuntu for instance) in the [Service] section of the /etc/systemd/system/my_service.service file:

[Unit]
After=multi-user.target

[Service]
Type=idle
ExecStart=/home/pi/securepi.sh
User=your_system_user

[Install]
WantedBy=multi-user.target
0

All I had to do was to add User=pi to fix this.

1
  • What/where did you add the User=pi?
    – simon
    Jan 31 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.