我有一个Python脚本,我想在启动时运行它。但是,当我将它作为systemd服务运行时,它似乎无法找到其中的一个模块。下面是状态消息:
● mqttproc.service - MQTT Post-processor
Loaded: loaded (/etc/systemd/system/mqttproc.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2021-12-06 11:10:45 GMT; 2min 31s ago
Process: 2375 ExecStart=/usr/bin/python3 /home/ken/python/mqttproc/mqttproc.py (code=exited, status=1/FAILURE)
Main PID: 2375 (code=exited, status=1/FAILURE)
Dec 06 11:10:45 MintVM systemd[1]: Started MQTT Post-processor.
Dec 06 11:10:45 MintVM python3[2375]: Sensor positions [100.0, 80.0, 60.0, 40.0, 20.0, 0.0] (Litres)
Dec 06 11:10:45 MintVM python3[2375]: Traceback (most recent call last):
Dec 06 11:10:45 MintVM python3[2375]: File "/home/ken/python/mqttproc/mqttproc.py", line 131, in <module>
Dec 06 11:10:45 MintVM python3[2375]: import paho.mqtt.client as mqtt
Dec 06 11:10:45 MintVM python3[2375]: ModuleNotFoundError: No module named 'paho'
Dec 06 11:10:45 MintVM systemd[1]: mqttproc.service: Main process exited, code=exited, status=1/FAILURE
Dec 06 11:10:45 MintVM systemd[1]: mqttproc.service: Failed with result 'exit-code'.奇怪的是,这个脚本是从命令行运行的。我甚至让它在另一台机器上作为系统服务运行。我的.service文件非常简单:
[Unit]
Description=MQTT Post-processor
[Service]
ExecStart=/usr/bin/python3 /home/ken/python/mqttproc/mqttproc.py
WorkingDirectory=/home/ken/python/mqttproc
[Install]
WantedBy=multi-user.target有人能指出我遗漏的明显的东西吗?
提前谢谢。
发布于 2021-12-06 14:23:16
通过cron运行的作业,或者systemd启动脚本不运行在桌面上相同的运行时环境中。systemd启动脚本以root的形式运行。您的PATH更改或来自~/.bashrc的其他环境变量设置都不会自动传播到cron作业。例如,没有$DISPLAY,所以GUI程序需要特殊处理(读man xhost)。
可以为cron文件中的所有crontab作业设置环境变量,读取man 5 crontab。
查看echo "=== id ===";id;echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias在每个环境中的结果。
https://unix.stackexchange.com/questions/680348
复制相似问题