英文:
Run python script on raspberry pi 4 on startup/reboot
问题
亲爱的Stackoverflow用户!
我正在努力找到一种在Raspberry Pi 4上启动和重新启动时执行我的Python脚本的方法。
这将是一个基本的基于Flask的API,用Python编写,用于通过Post/Get HTTP请求操作JSON数据库。
我已经尝试过使用crontab...
- 我的API.py脚本的路径是:/home/apiadmin/Deploy/API.py
- 在crontab文件中,我已经添加了这一行:@reboot python3 /home/apiadmin/API.py &
apiadmin应该是根用户
我可以成功保存crontab文件,在重新启动并使用 crontab -l 检查后,该行存在。
然而,在重新启动Raspberry Pi后,API不可访问/脚本未运行
当我使用PuTTY运行以下命令时:python3 /home/apiadmin/Deploy/API.py,脚本正常运行,API在线,每个请求都正常工作
我还尝试使用launcher.sh,并将其变为可执行文件,但失败了,显示无法进入目录(相同路径),因为它不存在。
还尝试过一开始使用30秒的延迟...没有解决问题。
提前感谢!
英文:
Dear People of Stackoverflow!
I am struggling to find a way to execute my python script on my Raspberry Pi4 on startup and reboot.
This would be a really basic Flask based API written in Python for manipulating a JSON Database with Post/Get HTTP requests.
I've already tried using crontab...
- The path to my API.py script is: /home/apiadmin/Deploy/API.py
- To the crontab file I've added the single line: @reboot python3 /home/apiadmin/API.py &
apiadmin supposed to be the root user
I can successfully safe the crontab file, upon rebooting and checking with crontab -l, the line is there.
However upon rebooting the Raspberry Pi the API is not reachable/script is not running
When I run it from PuTTY with the command: python3 /home/apiadmin/Deploy/API.py the script runs fine, and the API is on line, and every request works fine
Also I've tried using a launcher.sh, turning it to an executable but that fails saying it can't cd into the directory (same path) because it doesn't exist.
Also tried with a 30 second sleep to begin with... Didn't solve the problem.
Thank you in advance!
答案1
得分: 0
以下是翻译好的部分:
让我们使用 systemd
系统和 service
管理器,首先在 /etc/systemd/system 中创建一个新的服务,类似于 sudo nano /etc/systemd/system/api.service
然后在其中粘贴以下内容,它包含有关您的 API 的信息:
[Unit]
Description=我的 API 服务
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/apiadmin/Deploy/API.py
WorkingDirectory=/home/apiadmin/Deploy
StandardOutput=inherit
StandardError=inherit
Restart=always
User=apiadmin
[Install]
WantedBy=multi-user.target
然后我们保存并赋予它适当的权限 sudo chmod 644 /etc/systemd/system/api.service
。
现在,如果您想启动它,只需执行 sudo systemctl start api.service
,如果您想在启动时自动启动它,请执行 sudo systemctl enable api.service
。
英文:
Lets use the systemd
system and service
manager, first we create a new service in /etc/systemd/system, something like sudo nano /etc/systemd/system/api.service
Then paste this inside, it contain the information about your API:
[Unit]
Description=My API Service
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/apiadmin/Deploy/API.py
WorkingDirectory=/home/apiadmin/Deploy
StandardOutput=inherit
StandardError=inherit
Restart=always
User=apiadmin
[Install]
WantedBy=multi-user.target
THen we save and give it the proper permissions sudo chmod 644 /etc/systemd/system/api.service
.
Now if you want to start it just do sudo systemctl start api.service
and if you want to start it it on boot sudo systemctl enable api.service
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论