I have a Python script (worker.py) that I'd like to be running all the time. I searched for other options to make this work (such as daemonizing the process), but can't get that to work with my current Linux knowledge. So I figured that I could use cron to run pgrep to check if it was running, and if not, start it.
I've added a line to my crontab (
crontab -e) as this:
* * * * * pgrep -lf worker.py || /usr/bin/python /home/debian/hypermaq/scripts/worker.py
pgrep -lf worker.py from the command line returns an empty line if the script isn't running, and returns the correct PID if it is started in the other SSH session. So that makes me think the command is fine.
However, this doesn't work from cron. I've tried to find more info by piping the output of the pgrep command (added
>> /home/debian/templog.txt 2>&1), and I get a new PID on each line in that file. If I run the
pgrep -lf worker.py command with sudo, I do get a PID as well, which I think is the command I am running with sudo itself.
I have enabled the logging for cron, and can see that it starts the job each minute. I read that cron has a minimal path definition, so I used absolute paths.
I'm not sure how to interpret what I see, and thus, why it doesn't work as I expect. One specific thing I don't understand is why running the same
pgrep -lf worker.py command has no return when ran as regular user (and when the script isn't running), but returns a PID when ran with sudo, also when the script isn't running.