Skip to content Skip to sidebar Skip to footer

Run A Process Every Alternate X Hours

I have a python script which I am running like this as shown below: python3 ./bin/abc.py --log_file ./web/prr.log Now I need to make sure that above process runs after every 3 hou

Solution 1:

This seems like an ideal use case for crontab. I'd write 2 bash scripts that run every 3 hours via cron. So something like the following:

#ScriptA.sh
ifProcessRunning
    KillProcess
#ScriptB.sh
ifProcessNotRunning
    StartProcess
#CronTab
0,6,12,18 * ** * ScriptA.sh
3,9,15,21 * ** * ScriptB.sh

For killing the process you can use any normal unix command, so piping a ps aux and running kill within the shell script could work.

Hopefully this helps -- comment if you want me to flesh things out more!

Solution 2:

You can create a single bash script to get check if the python script is running. If it is, it will kill the script. If not, it will start the script.

#!/bin/bashif pgrep -f <program name> > /dev/null
    thenecho"Process is running. Killing"kill -9 $(pidof <program name>)
    elseecho"Process is not running. Starting"
         python <location of the file>
    fi

Post a Comment for "Run A Process Every Alternate X Hours"