Search code examples
pythoncron

Linux - Check if python script is running in "screen" and run if not


I am using "screen" to run a number of python scripts in AWS (linux build). One of those scripts will sometimes fail, for one reason or another. I would like to write either a) a python script or b) some kind of linux command line script to watch what python scripts are running, and if the target 'script.py' its not running then to rerun it.

For example:

import time
while(True):
    if script.py is running:
       time.sleep(5)
    else:
       open new linux screen
       python3 script.py
       detach screen

I'm sorry for this being vague, I never learned command line stuff, and I use it once in a blue moon. Screen took me a day to figure out how to use..

Thanks so much


Solution

  • ensure that flaky script keeps running

    Your proposed approach could be made to work. But a nanny script would be much simpler. Call it e.g. nanny.sh.

    #! /usr/bin/env bash
    
    while true
    do
        script.py
        sleep 1  # pause, so if script.py immediately dies we don't burn a core
    done
    

    Now we have replaced your "sometimes we randomly find script.py no longer running" situation with one where we're confident that the nanny is always running.

    Diagnosing / fixing script.py is left as an exercise for the reader. Fortunately it now is a less urgent matter.