0

I already searched several solutions on this site, but unfortunately the provided ones didn't work out for me.

Let's say I have a python script called "DataGen.py" that stops running (green arrow is clickable) because some background program is crashing. Unfortunately, there is no exception being thrown, which is why I need a workaround:

What I'm searching for is another python script called "RestartScript.py" that restarts "DataGen.py" 60 seconds after it has stopped running.

I tried something like:

from os import system
from time import sleep

while True:
    system('DataGen.py')
    sleep(300)
4

1 Answer 1

3

The system function executes what command you use as input.. See link. As such, this would be how you would run it to achieve the results you desire:

system('python DataGen.py')

Also, whatever value used as input in the sleep function is supposed to be seconds.. Using this logic, your code reruns every 300 seconds.

See below for full solution:

from os import system
from time import sleep

while True:
    system('python DataGen.py')
    sleep(60)
Sign up to request clarification or add additional context in comments.

2 Comments

It's a good start, but if you want to monitor DataGen.py from a master process, subprocess.call or the likes would probably be a better choice. Besides, there is no restart if crashed logic, which I would find much more interesting. I think a satisfying solution would be to pass DataGen.py's pid to a monitor function, that would regularly check if the process is still running, and restart it if it crashed. Maybe os.system('ps') then a lookup for this pid would be a good starter.
@Rightleg This would also be better for my purposes. The link "DocDriven" posted under the question is what you're refering to I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.