Automatically restart pending python scripts

Keywords: Python Linux Windows

Run the program, because of memory problems or other blabla blabla problems (not code problems altogether), the program may hang up occasionally, and we can't stare at the program all day. What can we do?It's a good idea to write a script to check if the program hangs up and restart if it does, depending on the operating system.

Method 1
A new script named run.sh can be created under linux:

#!/bin/sh
while [ 1 ]; do
  python program.py --params
done
  • 1
  • 2
  • 3
  • 4

Start as follows from the command line:

sh run.sh
  • 1

Where program.py is the python script to run, - params is the parameter.

A bat file can be created similarly under windows, but because bats are not very familiar, this part is left blank first.

Method 2
Add some extra code to python to check for exceptions, and if an exception occurs, re-execute it, using a recursive method.In the example below, I set count to a maximum of 3 to avoid infinite recursion.

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

Method 3
from Here Reference practices:

#!/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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Multithreaded
There is also a small multi-threaded experiment, you can see.

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

The output is as follows:

====================
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

Posted by besly98 on Sun, 05 Apr 2020 19:41:44 -0700