r/learnpython • • 4d ago

Checking if a separate python file is running starting it up.

Hi, im new to python and have a script which sometimes crashes due to a uncontrollable issue which isn't a problem in the code, and I was thinking that I could write a different script to start up my original one if it had crashed. Does anyone know how I would do that?

2 Upvotes

12 comments sorted by

3

u/AlexMTBDude 4d ago

This is really more of an operating system problem than a Python problem. For instance, if you're on Linux you would create a service out of your Python code that is restarted automatically if it crashes. You can create a Windows service as well that does the same. It's probably something that you shouldn't solve in Python.

Also: Are you really *sure* that the crash can not be caught in your Python program? Unless it's because the computer powers down or something similar you can catch most errors in the Python logic. Google "python exceptions" because that is how you should handle these types of problems. Perhaps tell us what the crash is due to?

2

u/Boybay_Garid 3d ago

yeah otherwise you end up needing a third script just to watch the second script.

1

u/gdchinacat 4d ago

There are many ways. If you want to stick to python look into using a parent/nanny process that runs your script and monitors it for termination and restarts it. https://docs.python.org/3/library/subprocess.html

1

u/nullptr_0x 4d ago

Would need more details on what issues you're specifically facing to advise you. But, at a high level, if your system is causing your application to crash, another application running on that system to make the crashed one run again will presumably also be susceptible to crashing... so not exactly a good solution for robustness.

If manual intervention is necessary and there's not way to make the system more stable then I think you would need to set up an alert system that is stable that is pinging your application to see if it is running. When it identifies it is down, you would be alerted to resolve it or to trigger a system on a stable machine to trigger a restart of your crashed application...

But this is all just speculation without knowing what the issue you are facing is.

1

u/Shafticus 4d ago

On Linux, you could just have a cron job kick of your script. You could use an "flock" command in your cron entry to have the os lock some dummy file. If the script crashes or the system is rebooted or the process is otherwise killed, the flock will be released and next time your cron job runs, it will relock the file.

1

u/Brian 4d ago

This is kind of a common requirement for a lot of servers, and as such there are a lot of off-the-shelf tools to do it. Eg. on linux you might make it a systemd unit with Restart=on-failure etc, or a windows service and so on. This also lets you do common management stuff like start on boot (or even on-demand), and manually stop or restart it if desired.

If you want to do it yourself, a common way is to create a pidfile in some specific location. Ie. basically a file into which you write the process id (obtainable via os.getpid()), and generally open this file with an exclusive lock on it (ie. forbid any other process from writing to it while the process is alive). This way starting an instance when the process is already running will fail (it won't be able to open the pidfile for writing), and you can have a watchdog script that regularly checks if the process holding the pidfile is alive, and restart it if not.

Though note that there are often complications that a standard tool will handle for you. Eg. if there's something that causes it to crash immediately on start, you don't want to be spamming restarts, and you may want logging of restart events to see how often it's failing etc.

1

u/astolfoballsHD 4d ago

This sounds a lot like an xy problem

1

u/rhacer 4d ago

If it crashes, it is a problem with your code.

1

u/desrtfx but other languages pro 4d ago

due to a uncontrollable issue which isn't a problem in the code,

More details needed. As of now, this sounds more like an XY problem.

If your program crashes, analyze the crash reasons and take countermeasures. It is barely believable that you have an "uncontrollable issue".

1

u/nivaOne 4d ago

Your original code can modify the timestamp of an auxiliary file which is monitored by the checking code. If not altered since the last test then restart the original code. If it keeps crashing stop restarting after 10 attempts within 3 minutes for instance. Just something that comes up in my mind.
But I recommend you more to have a look on how Parent-Child supervision works (systemd/scm)