0

I am creating a systemctl service for a python script which needs openvino environment initialized. Before running the python script, we have to initialize the openvino environment and have to run the python script from the same terminal in order to run it otherwise it will throw error. I have created below service for this:

[Unit]
Description=Launch application

[Service]
User=john
WorkingDirectory=/home/thingtrax/Documents/ThingTraxVisionPython
Environment=DISPLAY=:0
ExecStartPre=/opt/intel/openvino/bin/setupvars.sh
ExecStart=/usr/bin/python3 /home/john/Documents/ThingTraxVisionPython/rtsp_ttfr.py
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=graphical.target

Now as per my understanding, I am using ExecStartPre to initialize the openvino environment. OpenVino environment can not be initialized with root user and because we normally run systemctl service using root and this is why the python script is giving errors related to environment not initialized.

Is there any way we can mention everything to be run under john user. Please help. Thanks

1

EDIT: after understanding the actual issue

Based on the OpenVINO documentation: Install Intel® Distribution of OpenVINO™ toolkit for Linux.

Create a script which contains both commands:

#!/bin/bash
## /yourscript.sh
source /opt/intel/openvino/bin/setupvars.sh
/usr/bin/python3 /home/john/Documents/ThingTraxVisionPython

Then call this script from your service file:

[Unit]
Description=Launch application

[Service]
User=john
WorkingDirectory=/home/thingtrax/Documents/ThingTraxVisionPython
Environment=DISPLAY=:0
ExecStart=/yourscript.sh
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=graphical.target

First version

I'm not shure to completely understand your issue, but if you need the openvino service to be run as root and the ExecStartPre only to run as John, you can probably create a shell script that logs in as John and launched from ExecStartPre:

#!/bin/bash
## /yourscript.sh
su -l john -c '/opt/intel/openvino/bin/setupvars.sh' -

Note: don't forget the last dash at the end which will set the John environment variables.

Then add execution rights to it:

# chmod +x /yourscript.sh

End to finish, here is the service file afterwards:

[Unit]
Description=Launch application

[Service]
## removed the user param
WorkingDirectory=/home/thingtrax/Documents/ThingTraxVisionPython
Environment=DISPLAY=:0
ExecStartPre=/yourscript.sh ## changed the script to yourscript.sh
ExecStart=/usr/bin/python3 /home/john/Documents/ThingTraxVisionPython/rtsp_ttfr.py
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=graphical.target
8
  • Thanks for this idea of creating a shell script. I have question. When I start my service, I will have to use sudo systemctl start <service-name>. This include sudo so don't you think, its going to run everything under root user. I have tried your solution andgetting same error – S Andrew May 25 '20 at 9:53
  • it's uncorrelated, because systemd has its own daemon which runs as root (did not verify but highly likely). But please update your post and share the exact error log you have, it's hard to reason about it otherwise. Maybe your error is not related to that. Based on the installation instructions of openvino (docs.openvinotoolkit.org/latest/…), it's not enough to run the script, you need to make it run in the same bash instance as the startup script itself – Musinux May 25 '20 at 10:03
  • You should try to create a script that launches both the setupvars.sh and rtsp_ttfr.py and replace your ExecStart with this script. Therefore, the env variables will be set when rtsp_ttfr.py will be called (See my edit) – Musinux May 25 '20 at 10:07
  • Let me try few of the solutions you suggested and will update you – S Andrew May 25 '20 at 10:14
  • @SAndrew did it work for you in the end ? – Musinux May 26 '20 at 9:37
0

As systemctl run as root you can use sudo to switch to another user without password prompt. Example:

root@leo-pc:~# sudo -u leonid bash
leonid@leo-pc:~$ 

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.