TransWikia.com

How to run a command to before starting systemctl service

Ask Ubuntu Asked on December 8, 2021

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

2 Answers

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:~$ 

Answered by LeonidMew on December 8, 2021

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

Answered by Musinux on December 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP