简体   繁体   English

在远程服务器上连续运行并重新启动python脚本

[英]Continuously running and restarting python scripts on a remote server

I have 3-4 python scripts which I want to run continuously on a remote server(which i am accessing through ssh login).The script might crash due to exceptions(handling all the exceptions is not practical now), and in such cases i want to immediately restart the script. 我有3-4个python脚本,要在远程服务器上连续运行(我正在通过ssh登录访问)。该脚本可能由于异常而崩溃(处理所有异常现在不实际),在这种情况下,我想要立即重新启动脚本。
What i have done till now. 到现在为止我所做的。
I have written a .conf file in /etc/init 我在/ etc / init中写了一个.conf文件

chdir /home/user/Desktop/myFolder chdir / home /用户/桌面/ myFolder
exec python myFile.py exec python myFile.py
respawn 重生

This seemed to work fine for about 4 hours and then it stopped working and i could not start the .conf file. 这似乎可以正常工作约4个小时,然后它停止工作,并且我无法启动.conf文件。
Suggest changes to this or i am also open to a new approach 建议对此进行更改,否则我也欢迎采用新方法

Easiest way to do it - run in infinite bash loop in screen. 最简单的方法-在屏幕上无限bash循环中运行。 Also it's the worst way to do it: 这也是最糟糕的方法:

screen -S sessionName bash -c 'while true; python myFile.py ; done'

You can also use http://supervisord.org/ or daemon by writing init.d script for it http://www.linux.com/learn/tutorials/442412-managing-linux-daemons-with-init-scripts 您也可以通过为其编写init.d脚本来使用http://supervisord.org/或守护程序http://www.linux.com/learn/tutorials/442412-managing-linux-daemons-with-init-scripts

If your script is running on an Ubuntu machine you have the very convenient Upstart, http://upstart.ubuntu.com/ 如果您的脚本在Ubuntu计算机上运行,​​那么您将拥有非常方便的Upstart, 网址为http://upstart.ubuntu.com/

Upstart does a great job running services on boot and respawning a process that died. Upstart在启动时运行服务并重新生成已终止的进程方面做得很好。

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

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