systemd execstart python 守护进程动态使用来自环境变量的 virtualenv

systemd execstart python daemon dynamically using virtualenv from environment variable

我有 Python 脚本,在 CentOS 7 中用作 systemd 守护进程。守护进程由我在 virtualenv 中创建的 python 版本执行。我正在尝试调整脚本以能够在环境变量中设置 virtualenv 路径,这样我就可以通过一个变量更改路径并重新启动服务来轻松切换到不同的 virtualenv。我已经创建了我的 systemd 脚本,以便能够初始化守护进程的多个实例,这非常有效。当我尝试使用环境变量指向我的 python 解析器时,事情发生了变化。这是我目前所拥有的。

/etc/systemd/system/pipeline-remove@.service:

[Unit]
Description=pipeline remove tickets worker instances as a service, instance %i
Requires=pipeline-remove.service
Before=pipeline-remove.service
BindsTo=pipeline-remove.service

[Service]
PermissionsStartOnly=true
Type=idle
User=root
ExecStart=/path/to/venv/bin/python /pipeline/python/daemons/remove_tickets.py
Restart=always
TimeoutStartSec=10
RestartSec=10

[Install]
WantedBy=pipeline-remove.service

/etc/systemd/system/pipeline-remove.service(启动所有实例):

[Unit]
Description=manages pipeline remove tickets worker instances as a service, instance

[Service]
Type=oneshot
ExecStart=/usr/bin/sh /usr/bin/pipeline-remove-start.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

管道-删除-start.sh:

#!/bin/bash
systemctl start pipeline-remove@{1..2}

这对我来说非常有用,但是当我尝试按以下方式设置 python 目录时出现问题:

/etc/profile.d/pipeline_envvars.sh:

PIPELINE_VIRTUALENV=/path/to/venv

/etc/systemd/system/pipeline-remove@.service:

[Unit]
Description=pipeline remove tickets worker instances as a service, instance %i
Requires=pipeline-remove.service
Before=pipeline-remove.service
BindsTo=pipeline-remove.service

[Service]
PermissionsStartOnly=true
Type=idle
User=root
EnvironmentFile=/etc/profile.d/pipeline_envvars.sh
ExecStart=/${PIPELINE_VIRTUALENV}/bin/python /pipeline/python/daemons/remove_tickets.py
Restart=always
TimeoutStartSec=10
RestartSec=10

[Install]
WantedBy=pipeline-remove.service

然后我尝试启动它:

sudo systemctl daemon-reload
sudo systemctl restart pipeline-remove@{1..1}
sudo systemctl status pipeline-remove@{1..1}

状态显示以下退出代码 203,表示未找到可执行文件:

● pipeline-remove@1.service - pipeline remove tickets worker instances as a service, instance 1
   Loaded: loaded (/etc/systemd/system/pipeline-remove@.service; disabled; vendor preset: disabled)
   Active: activating (auto-restart) (Result: exit-code) since Fri 2018-01-26 15:04:50 UTC; 6s ago
  Process: 11716 ExecStart=/${PIPELINE_VIRTUALENV}/bin/python /pipeline/python/daemons/remove_tickets.py (code=exited, status=203/EXEC)
 Main PID: 11716 (code=exited, status=203/EXEC)

Jan 26 15:04:50 dev systemd[1]: pipeline-remove@1.service: main process exited, code=exited, status=203/EXEC
Jan 26 15:04:50 dev systemd[1]: Unit pipeline-remove@1.service entered failed state.
Jan 26 15:04:50 dev systemd[1]: pipeline-remove@1.service failed.

找到执行代码 here。在系统日志中也发现了这个,/var/log/messages:

Jan 26 15:07:13 dev systemd: Starting pipeline remove tickets worker instances as a service, instance 1...
Jan 26 15:07:13 dev systemd: Failed at step EXEC spawning /${PIPELINE_VIRTUALENV}/bin/python: No such file or directory
Jan 26 15:07:13 dev systemd: pipeline-remove@1.service: main process exited, code=exited, status=203/EXEC
Jan 26 15:07:13 dev systemd: Unit pipeline-remove@1.service entered failed state.
Jan 26 15:07:13 dev systemd: pipeline-remove@1.service failed.
Jan 26 15:07:23 dev systemd: pipeline-remove@1.service holdoff time over, scheduling restart.
Jan 26 15:07:23 dev systemd: Started pipeline remove tickets worker instances as a service, instance 1.

当我尝试删除 ExecStart 中的前导 / 时,即使我的环境变量确实包含绝对路径,我也会收到相对路径错误:

Failed to start pipeline-remove@1.service: Unit is not loaded properly: 
Invalid argument.
See system logs and 'systemctl status pipeline-remove@1.service' for 
details.

状态显示如下:

vagrant@dev:~$ sudo systemctl status pipeline-remove@{1..1}
● pipeline-remove@1.service - pipeline remove tickets worker instances as a service, instance 1
   Loaded: error (Reason: Invalid argument)
   Active: inactive (dead)

Jan 26 15:11:39 dev systemd[1]: pipeline-remove@1.service failed.
Jan 26 15:11:42 dev systemd[1]: Stopped pipeline remove tickets worker instances as a service, instance 1.
Jan 26 15:11:42 dev systemd[1]: [/etc/systemd/system/pipeline-remove@.service:12] Executable path is not absolute, ignoring: ${PIPELINE_VIRTUALENV}/bin/python /pipel...e_tickets.py
Jan 26 15:11:42 dev systemd[1]: pipeline-remove@1.service lacks both ExecStart= and ExecStop= setting. Refusing.

我使用 this 指南帮助我开始,但现在我被卡住了。如何在从环境变量设置 python 可执行路径时启动 python 守护程序?

经过更多阅读,我偶然发现了答案 here。问题是 ExecStart 的第一个参数必须是文字:

ExecStart= Commands with their arguments that are executed when this service is started. For each of the specified commands, the first argument must be an absolute and literal path to an executable.

在 ExecStart 上进一步阅读它说:

Variables whose value is not known at expansion time are treated as empty strings. Note that the first argument (i.e. the program to execute) may not be a variable.

我最终也偶然发现了这个 answer,这看起来是同一个问题。最后这是有效的:用 shell 包装整个东西到 运行:

[Unit]
Description=pipeline remove tickets worker instances as a service, instance %i
Requires=pipeline-remove.service
Before=pipeline-remove.service
BindsTo=pipeline-remove.service

[Service]
PermissionsStartOnly=true
Type=idle
User=root
EnvironmentFile=/etc/profile.d/pipeline_envvars.sh
ExecStart=/bin/sh -c '${PIPELINE_VIRTUALENV}/bin/python /pipeline/python/daemons/remove_tickets.py'
Restart=always
TimeoutStartSec=10
RestartSec=10

[Install]
WantedBy=pipeline-remove.service

所以 ExecStart=/bin/sh -c '<your command>' 节省了时间,我现在可以为我的环境变量指定 python 解释器路径。将留下答案,希望能为其他人节省一些时间。