AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1243412
Accepted
S Andrew
S Andrew
Asked: 2020-05-26 00:59:01 +0800 CST2020-05-26 00:59:01 +0800 CST 2020-05-26 00:59:01 +0800 CST

如何在启动 systemctl 服务之前运行命令

  • 772

我正在systemctl为需要openvino初始化环境的 python 脚本创建服务。在运行 python 脚本之前,我们必须初始化openvino环境,并且必须从同一个终端运行 python 脚本才能运行它,否则会抛出错误。我为此创建了以下服务:

[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

现在根据我的理解,我正在使用ExecStartPre初始化 openvino 环境。OpenVino 环境不能用 root 用户初始化,因为我们通常使用 root 运行 systemctl 服务,这就是为什么 python 脚本给出与未初始化环境相关的错误。

有什么方法可以提到要在 john 用户下运行的所有内容。请帮忙。谢谢

scripts command-line python services systemd
  • 2 2 个回答
  • 6078 Views

2 个回答

  • Voted
  1. Best Answer
    Musinux
    2020-05-26T01:17:40+08:002020-05-26T01:17:40+08:00

    编辑:了解实际问题后

    基于 OpenVINO 文档:安装适用于 Linux 的英特尔® Distribution of OpenVINO™ toolkit。

    创建一个包含两个命令的脚本:

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

    然后从您的服务文件中调用此脚本:

    [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
    

    第一个版本

    我不完全理解您的问题,但是如果您需要以 root 身份运行 openvino 服务并且仅以 John 身份运行 ExecStartPre,您可以创建一个以 John 身份登录并从 ExecStartPre 启动的 shell 脚本:

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

    注意:不要忘记末尾的最后一个破折号,它将设置 John 环境变量。

    然后为其添加执行权限:

    # chmod +x /yourscript.sh
    

    结束,这里是之后的服务文件:

    [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
    
    • 1
  2. LeonidMew
    2020-05-26T01:20:04+08:002020-05-26T01:20:04+08:00

    由于 systemctl 以 root 身份运行,您可以使用 sudo 切换到另一个用户而无需密码提示。例子:

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

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve