用systemd启动的python无法启动子进程 [英] python initiated with systemd cannot start subprocess

查看:33
本文介绍了用systemd启动的python无法启动子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 systemd 启动的 virtualenv 中有一个 python 脚本.

I have a python script inside a virtualenv which is started using systemd.

[Unit]
Description=app
After=network.target

[Service]
Type=simple
User=user
Group=user
Environment=VIRTUAL_ENV=/home/user/Projects/app/venv
Environment=PATH=$VIRTUAL_ENV/bin:$PATH
WorkingDirectory=/home/user/Projects/app
ExecStart=/home/user/Projects/app/venv/bin/python app.py

[Install]
WantedBy=multi-user.target

问题是脚本使用 subprocess.Popen(['python', 'whatever.py']) 打开另一个 python 脚本.我得到了一个 not found 错误,发现应该用绝对路径调用 python,所以我改变了它,它运行良好.

The thing is that the script uses subprocess.Popen(['python', 'whatever.py']) to open another python script. I got a not found error, and discovered that python should be invoked with an absolute path, so I changed it and it worked well.

但是,现在我使用第三方库,pygatt,它内部使用子进程打开 gatttoolhcitool 中的 >$PATH(系统范围的二进制文件,通常在/usr/bin 中).
所以现在我不能改变那个库(我可以通过分叉它,但我希望我不必这样做).

However, now I use a third party library, pygatt, which inside uses subprocess to open gatttool or hcitool which are in $PATH (system wide binaries, usually in /usr/bin).
So now I cannot change that library (I could by forking it, but I hope I don't have to).

为什么 systemd 不能在不使用绝对路径的情况下生成 python 子进程?没有 systemd(从控制台运行),一切正常.

How comes that systemd cannot spawn python subprocesses without using an absolute path? Without systemd (running from console), everything works.

推荐答案

我不确定,但很可能在以下配置行中没有考虑到一个配置行中的设置环境.

I'm not sure but it is very possible that setting environment in one configuration line isn't taken into account in the following ones.

Environment=VIRTUAL_ENV=/home/user/Projects/app/venv
Environment=PATH=$VIRTUAL_ENV/bin:$PATH

在这里,您期望将 VIRTUAL_ENV 设置为 $VIRTUAL_ENV 在下一行进行评估,但这可能不起作用.我会尝试硬编码第二行:

here you're expecting VIRTUAL_ENV to be set to $VIRTUAL_ENV is evaluated the next line, but that may not work. I would try hardcoding the second line:

Environment=VIRTUAL_ENV=/home/user/Projects/app/venv
Environment=PATH=/home/user/Projects/app/venv/bin:$PATH

这篇关于用systemd启动的python无法启动子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆