Preventing “Overlapping” cron Processes
I have a number of very time consuming processes that get run by cron on various machines. Some of these processes would cause problems if they “overlapped” — e.g., a new one gets started before the old one is done.
Now, there are a lot of ways to make sure you’re unique if you’re a process, but often I don’t want to modify the source of the process to add that (for many packages, I’d rather not patch and merge and recompile every time a new version comes out). So I write a simple shell script to run the process; cron calls my shell script, and prevents the overlap.
This uses the magic of “pgrep” — unfortunately, different versions of pgrep have different flags, so the code I originally wrote (which used the “-c” flag, which counts the matching processes) didn’t port to most systems. It’s easy enough to pipe the output through a “wc -l”.
I did have to move the pgrep exec out of my if statement, though, since the comparison was going against the return code, not the output. Doh!
#!/bin/bash RUNNING_PROCS = `pgrep -f longRunningProcess | wc -l` if [ "$RUNNING_PROCS" -gt "0" ] then echo `date` longRunningProcess still running. I\'ll let it finish. else echo `date` Starting longRunningProcess. /path/to/longRunningProcess -flags fi echo "----------------------------"
Leave a Reply