-2

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

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
keynesiancross
  • 3,441
  • 15
  • 47
  • 87

1 Answers1

1

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.

J_H
  • 17,926
  • 4
  • 24
  • 44
  • I dont really understand what thats doing? how does it test if its alive, and restart it if it dies? – keynesiancross Jul 01 '22 at 23:55
  • 1
    Try it and see! Replace the "script.py" command with e.g. `date`, and notice that the command exits at once so we see one-second timestamps. Now replace it with `bash -c "date; sleep 9; date"`, and notice that the command runs for much longer but eventually dies and the nanny must restart it. The technical answer to your question is bash calls `wait4()` to detect child death. The simple answer is bash runs each command in turn, waiting for child to exit before moving on to next command. https://linux.die.net/man/2/wait4 – J_H Jul 02 '22 at 00:08
  • A bit late comin back on this - but is the syntax above correct? Does it need "Do" in it? My IDE doesn't seem to like it, but I'm not sure if I'm looking at the right info online. When I try to run it, I get "unexpected end of file" – keynesiancross Jul 11 '22 at 14:00
  • D'oh, fail! Thank you for pointing that out. I was thinking in two languages at the same time, python + bash, not a good mix. Fixed. – J_H Jul 12 '22 at 03:53