python initiated with systemd cannot start subprocess

半世苍凉 提交于 2019-12-11 06:39:08

问题


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

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.

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).

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


回答1:


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

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


来源:https://stackoverflow.com/questions/48371272/python-initiated-with-systemd-cannot-start-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!