Search code examples
pythonrestart

Restart python script from other script


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)

Solution

  • 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)