简体   繁体   English

在启动时运行python脚本无缓冲?

[英]running python script unbuffered on boot?

I have a script that runs in the background, and it is supposed to run 24/7, but some times it crashes. 我有一个在后台运行的脚本,它应该全天候运行,但有时会崩溃。 But it's running on my headless pi, so I don't get to see the error messages when it crashes. 但它正在我无头的pi上运行,所以当它崩溃时我没有看到错误消息。
I found a solution where I can have the script print to a file "unbuffered" 我找到了一个解决方案,我可以将脚本打印到文件“unbuffered”

python -u my_background_script.py > errorfile.txt &

This works, if I activate it manually through ssh. 如果我通过ssh手动激活它,这是有效的。 But I want it to run on bootup, so I added that line to my /etc/rc.local and now it won't run. 但我希望它在启动时运行,所以我将该行添加到我的/etc/rc.local中,现在它不会运行。 It runs without the "-u", but then it won't write output. 它运行时没有“-u”,但它不会写输出。
Am I stuck with using some sort of logging module? 我是否坚持使用某种记录模块? Thanks 谢谢

Take a look at Supervisor . 看看主管 Pretty sure it's exactly what you're looking for. 很确定它正是你正在寻找的。

Here's a great tutorial for it too: https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps 这里有一个很棒的教程: https//www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps

Hope this helps! 希望这可以帮助!

I would add as a cronjob. 我会添加一个cronjob。 Use 采用

sudo contab -e

and add 并添加

@reboot /path/to/pythonscript

Also make sure the python file was made executable with 还要确保python文件是可执行的

chmod +x myfile.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM