0

I'm fairly new to Python.

I'm running a python script on Linux named "locationGPIO2.py". I'm trying to write a separate script to monitor whether "locationGPIO2.py" is running at all times.

I want this separate script to basically be looping and checking if "locationGPIO2.py" is running, if it is, then do nothing, but if it is no longer running (either by user stopping the script or by crashing) then change a variable named 'heartbeat' to "Inactive".

I've been looking around for a solution for a while, but can't find anything that works. Could anybody help me implement this into my script?

Here's what I imagine the script should do:

ENTER CHECK IF SCRIPT IS RUNNING CODE HERE
If locationGPIO2.py is NOT running THEN:
heartbeat = "Inactive"
Else:
Do nothing
  • As an idea, the ‘checker’ script can be triggered at a time interval via cron, and perform the requested task. Or, just write a simple ‘checker’ script in bash, and pop that into cron. – S3DEV Jun 14 '22 at 11:43
  • Hi, I'm really new to python, I'm looking for some sort of solution on how to code the "Check" part of the script. Not sure how to check if the script is running in bash or a normal python script – Harley Hartwell Jun 14 '22 at 11:45
  • Perhaps [this questions (and answers)](https://stackoverflow.com/q/696839/6340496) might be of help. Seems to be exactly what you're after. – S3DEV Jun 14 '22 at 11:51
  • Additionally, [this link](https://www.howtogeek.com/687970/how-to-run-a-linux-program-at-startup-with-systemd/) provides guidance for setting up the Python script as a background *service*, via systemd. The config file can be set such that the Python script will be restarted automatically on failure. Thus, forget the 'checker' script all-together. – S3DEV Jun 14 '22 at 11:57
  • The main purpose of this checker script is to alert the user if the script has stopped, not to always restart it each time it crashes. I have a GUI script that uses the "Heartbeat" variable to display the status of the locationGPIO2 script, which is why I'm trying to get the tracker script to detect when it fails to then change the GUI to display that the script has stopped. – Harley Hartwell Jun 14 '22 at 13:50

0 Answers0