Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

raspberry pi - systemd service not starting on boot, starts when i restart it

I have made this service file to start a python script when my raspberry pi (4) boots up:

/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

However, once the pi is on, I run sudo systemctl status plants, and get:

* 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.

But, after running sudo systemctl restart plants, the service starts up and everything is fine.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If it doesn't start on boot but does on systemctl restart, I'd be looking at whether /home/theo/Repos/plants-monitor/remote is mounted at that point.

There may be something automounting or home-mounting your home directory when you log in.

If so, you could change the working directory to something that exists always, even if only a test.

Additionally, using journalctl -n 9999 -u plants will get you more log messages, so you can see why it's failing, rather than just seeing the "tried too many times, giving up" messages.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...