当我的树莓派(4)启动时,我已经制作了此服务文件以启动python脚本:
/etc/systemd/system/plants.service
[Unit]
Description=plant-sender
After=network.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/home/theo/Repos/plants-monitor/remote
ExecStart=/usr/bin/python main.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
但是,一旦打开pi,我运行sudo systemctl status plants
,并得到:
* plants.service - plant-sender
Loaded: loaded (/etc/systemd/system/plants.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2020-03-30 20:22:43 EDT; 1min 45s ago
Process: 323 ExecStart=/usr/bin/python main.py (code=exited, status=1/FAILURE)
Main PID: 323 (code=exited, status=1/FAILURE)
Mar 30 20:22:43 arpi systemd[1]: plants.service: Scheduled restart job, restart counter is at 5.
Mar 30 20:22:43 arpi systemd[1]: Stopped plant-sender.
Mar 30 20:22:43 arpi systemd[1]: plants.service: Start request repeated too quickly.
Mar 30 20:22:43 arpi systemd[1]: plants.service: Failed with result 'exit-code'.
Mar 30 20:22:43 arpi systemd[1]: Failed to start plant-sender.
但是,运行sudo systemctl restart plants
后,服务启动,一切都很好。
[如果它不是在启动时启动,但在systemctl restart
上是does,那么我将查看是否在该点安装了/home/theo/Repos/plants-monitor/remote
。
登录时可能会有自动挂载或主目录挂载的内容。
如果是这样,您可以将工作目录更改为始终存在的目录,即使只是进行测试。
此外,使用journalctl -n 9999 -u plants
将为您提供更多日志消息,因此您可以看到为什么失败,而不仅仅是看到“尝试了太多次,放弃了”消息。