自动重启挂掉的python脚本

2023-01-31 01:01:16 python 脚本 自动重启

跑程序,因为内存问题或者其它blabla问题(总之不是代码问题),程序可能会偶尔挂掉,我们又不能整天盯着程序,怎么办呢?写个脚本来检查程序是否挂掉,如果挂掉就重启,这是一个不错的想法,具体做法依操作系统而不同。

方法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

相关文章