自动重启挂掉的python脚本
2015-05-09 13:30
681 查看
跑程序,因为内存问题或者其它blabla问题(总之不是代码问题),程序可能会偶尔挂掉,我们又不能整天盯着程序,怎么办呢?写个脚本来检查程序是否挂掉,如果挂掉就重启,这是一个不错的想法,具体做法依操作系统而不同。
方法1
在linux下可以新建一个名为run.sh的脚本:
在命令行中这样启动:
其中program.py是要运行的python脚本,–params是参数。
在windows下可以类似地建个bat文件,由于bat不太熟,所以这部分就先空着。
方法2
在python中增加一些额外检查异常的代码,如果发生异常,就重新执行,这里用的是递归的方法。下面的例子中,我设置count最大为3,为了避免无限递归下去。
方法3
从这里 借鉴的做法:
多线程
另外还做了个多线程的小实验,大家可以看看。
输出如下:
方法1
在linux下可以新建一个名为run.sh的脚本:
#!/bin/sh while [ 1 ]; do python program.py --params done
在命令行中这样启动:
sh run.sh
其中program.py是要运行的python脚本,–params是参数。
在windows下可以类似地建个bat文件,由于bat不太熟,所以这部分就先空着。
方法2
在python中增加一些额外检查异常的代码,如果发生异常,就重新执行,这里用的是递归的方法。下面的例子中,我设置count最大为3,为了避免无限递归下去。
import time count = 0 def compute_number(): for i in xrange(10): print 'count number: %s' % str(i+1) time.sleep(1) raise Exception("a", "b") def main(): print "AutoRes is starting" print "Respawning" global count if count < 3: try: count += 1 compute_number() except Exception, e: print e main() finally: print 'success' if __name__ == "__main__": main()
方法3
从这里 借鉴的做法:
#!/usr/bin/env python import os, sys, time def main(): print "AutoRes is starting" executable = sys.executable args = sys.argv[:] args.insert(0, sys.executable) time.sleep(1) print "Respawning" os.execvp(executable, args) if __name__ == "__main__": main()
多线程
另外还做了个多线程的小实验,大家可以看看。
import time from multiprocessing import Pool count = 0 def compute_number(num): for i in xrange(3): print 'current process = %s, count number: %s' % (str(num),str(i+1)) time.sleep(1) raise Exception("a", "b") def main(num): print '====================' print "current process = %d" % num print "Respawning" global count if count < 2: try: count += 1 compute_number(num) except Exception, e: print e main(num) finally: print 'success' if __name__ == "__main__": pool = Pool(2) pool.map(main,[1,2]) pool.close() pool.join()
输出如下:
==================== current process = 1 Respawning current process = 1, count number: 1 ==================== current process = 2 Respawning current process = 2, count number: 1 current process = 1, count number: 2 current process = 2, count number: 2 current process = 1, count number: 3 current process = 2, count number: 3 ('a', 'b') ==================== current process = 1 Respawning current process = 1, count number: 1 ('a', 'b') ==================== current process = 2 Respawning current process = 2, count number: 1 current process = 1, count number: 2 current process = 2, count number: 2 current process = 1, count number: 3 current process = 2, count number: 3 ('a', 'b') ==================== current process = 1 Respawning success success ('a', 'b') ==================== current process = 2 Respawning success success
相关文章推荐
- 自动重启挂掉的python脚本
- 【Shell/Python】Linux 进程自动监控bash shell脚本 监控VPS服务器负载/CPU及内存占用 自动重启
- python脚本自动监视重启tomcat进程
- Memcached进程挂掉自动重启脚本
- linux下监视进程 崩溃挂掉后自动重启的shell脚本
- linux下监视进程 崩溃挂掉后自动重启的shell脚本
- linux下监视进程 崩溃挂掉后自动重启的shell脚本
- tomcat挂掉后自动重启脚本
- 迅雷下载最近经常异常崩溃,下个Python脚本自动监控重启之
- 实验室中一段监听某程序结束后自动重启该改程序的Python脚本
- python脚本自动监视tomcat进程,如果崩溃就自动重启
- Memcached进程挂掉自动重启脚本
- Memcached进程挂掉自动重启脚本
- 【原】用shell脚本监视进程,崩溃挂掉后自动重启
- python脚本自动监视tomcat进程,如果崩溃就自动重启
- python tab自动补全脚本
- Python自动发送邮件的脚本源码
- 自动监控apache服务状态并重启的shell脚本
- python脚本监控进程,服务停止后自动启动
- python脚本设置微信自动回复[简单应用]