gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:41:31 25 4
gpt4 key购买 nike

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

/etc/systemd/system/[email protected] :

[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

管道删除启动.sh:

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

这对我来说非常有用,但是当我尝试按以下方式设置 python 目录时,事情就会崩溃:

/etc/profile.d/pipeline_envvars.sh:

PIPELINE_VIRTUALENV=/path/to/venv

/etc/systemd/system/[email protected] :

[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,这意味着未找到可执行文件:

● <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6a6bfa6b3babfb8b3fba4b3bbb9a0b396e7f8a5b3a4a0bfb5b3" rel="noreferrer noopener nofollow">[email protected]</a> - pipeline remove tickets worker instances as a service, instance 1
Loaded: loaded (/etc/systemd/system/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b5b425b4e4742454e06594e46445d4e6b05584e595d42484e" rel="noreferrer noopener nofollow">[email protected]</a>; 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]: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2a2bba2b7bebbbcb7ffa0b7bfbda4b792e3fca1b7a0a4bbb1b7" rel="noreferrer noopener nofollow">[email protected]</a>: main process exited, code=exited, status=203/EXEC
Jan 26 15:04:50 dev systemd[1]: Unit <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f3839a83969f9a9d96de81969e9c8596b3c2dd809681859a9096" rel="noreferrer noopener nofollow">[email protected]</a> entered failed state.
Jan 26 15:04:50 dev systemd[1]: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="37475e47525b5e59521a45525a584152770619445245415e5452" rel="noreferrer noopener nofollow">[email protected]</a> 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: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="55253c2530393c3b30782730383a233015647b263027233c3630" rel="noreferrer noopener nofollow">[email protected]</a>: main process exited, code=exited, status=203/EXEC
Jan 26 15:07:13 dev systemd: Unit <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56263f26333a3f38337b24333b392033166778253324203f3533" rel="noreferrer noopener nofollow">[email protected]</a> entered failed state.
Jan 26 15:07:13 dev systemd: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dbabb2abbeb7b2b5bef6a9beb6b4adbe9beaf5a8bea9adb2b8be" rel="noreferrer noopener nofollow">[email protected]</a> failed.
Jan 26 15:07:23 dev systemd: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72021b02171e1b1c175f00171f1d041732435c011700041b1117" rel="noreferrer noopener nofollow">[email protected]</a> 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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee9e879e8b8287808bc39c8b8381988baedfc09d8b9c98878d8b" rel="noreferrer noopener nofollow">[email protected]</a>: Unit is not loaded properly: 
Invalid argument.
See system logs and 'systemctl status <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bacad3cadfd6d3d4df97c8dfd7d5ccdffa8b94c9dfc8ccd3d9df" rel="noreferrer noopener nofollow">[email protected]</a>' for
details.

状态显示如下:

vagrant@dev:~$ sudo systemctl status pipeline-remove@{1..1}
● <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="03736a73666f6a6d662e71666e6c756643322d706671756a6066" rel="noreferrer noopener nofollow">[email protected]</a> - 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]: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b4b524b5e5752555e16495e56544d5e7b0a15485e494d52585e" rel="noreferrer noopener nofollow">[email protected]</a> 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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64140d1401080d0a01491601090b1201244a170116120d0701" rel="noreferrer noopener nofollow">[email protected]</a>:12] Executable path is not absolute, ignoring: ${PIPELINE_VIRTUALENV}/bin/python /pipel...e_tickets.py
Jan 26 15:11:42 dev systemd[1]: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfafb6afbab3b6b1baf2adbab2b0a9ba9feef1acbaada9b6bcba" rel="noreferrer noopener nofollow">[email protected]</a> 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 解释器路径。将留下答案,希望能为其他人节省一些时间。

关于python - systemd execstart python 守护进程动态使用环境变量中的 virtualenv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48464270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com