问题
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