使用systemd防止python脚本崩溃

使用systemd防止python脚本崩溃

我想防止多个 python 脚本在我的 Raspberry Pi(Stretch OS)上崩溃。

我想澄清这是否是实现 systemd 文件以防止多种的 Python避免脚本崩溃。在服务文件的服务部分下,典型格式为:

[Service]
ExecStart=/path/too/script
Restart=always

但由于我正在跑步多条蟒蛇脚本,我想我应该将 python 添加到路径的前面,并将服务部分堆叠在一起。如果我错了,请纠正我。

我当前的脚本(constantrun.service)是:

[Unit]
Description='python scripts that needs to be run constantly'

[Service]
ExecStart=python /home/pi/projects/script1.py
Restart=always
[Service]
ExecStart=python /home/pi/projects/script2.py
Restart=always
[Service]
ExecStart=python /home/pi/projects/script3.py
Restart=always

[Install]
WantedBy=multi-user.target

但是,当我运行时尝试使用sudo systemctl start constantrun.service.我收到以下错误:

Failed to start constantrun.service: Unit constantrun.service is not loaded properly: Invalid argument.
See system logs and 'systemctl status constantrun.service' for details.

我打开日志,看到:

● constantrun.service - 'python scripts that needs to be run constantly'
   Loaded: error (Reason: Invalid argument)
   Active: inactive (dead)

Feb 18 17:15:12 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:11] Executable path is not absolute, ignoring: python /home/pi/P
Feb 18 17:15:12 raspberrypi systemd[1]: constantrun.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Feb 18 17:20:17 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:5] Executable path is not absolute, ignoring: python /home/pi/Pr
Feb 18 17:20:17 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:8] Executable path is not absolute, ignoring: python /home/pi/Pr
Feb 18 17:20:17 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:11] Executable path is not absolute, ignoring: python /home/pi/P
Feb 18 17:20:17 raspberrypi systemd[1]: constantrun.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Feb 18 17:20:33 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:5] Executable path is not absolute, ignoring: python /home/pi/Pr
Feb 18 17:20:33 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:8] Executable path is not absolute, ignoring: python /home/pi/Pr
Feb 18 17:20:33 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:11] Executable path is not absolute, ignoring: python /home/pi/P
Feb 18 17:20:33 raspberrypi systemd[1]: constantrun.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.

如何修改上面的代码才能使其正常工作?另外,我是否需要添加我的 python 库的路径,例如PYTHONPATH=/home/pi/.local/lib/python2.7/site-packages

我对 Linux 环境真的很陌生,非常感谢对此的任何建议!

答案1

您的服务定义如下:

[Service]
ExecStart=python /home/pi/projects/script1.py
Restart=always

这是每个错误消息:

Executable path is not absolute, ignoring: python /home/pi/...

对于systemd,该服务定义中的“可执行文件”是python,这显然不是绝对路径。这/home/pi/projects/script1.py只是这个python可执行文件的一个参数,关心它的正确形式是可执行文件的工作。

任何特定于 Python 的环境变量(例如:您)PYTHONPATH根本没有任何意义systemd必须每次都为其提供服务定义中可执行文件的绝对路径。

通常,python 解释器的绝对路径是/usr/bin/python,但您可以检查:

$ type python
python is /usr/bin/python

所以你的服务定义应该是这样的:

[Service]
ExecStart=/usr/bin/python /home/pi/projects/script1.py
Restart=always

可以在这里找到有关 systemd 的非常有用的介绍。它获取并保持 Telegram 机器人作为 Python 脚本运行。

相关内容