Linux – check if process is running, even under cron

bashcentoscronlinux

I have a cron job that runs every minute, but if it finds data, might take many minutes to finish.

I need to tell if another instance of the script is running from another. To make things even more fun, we call the same script, with different parameters.. so for example:

* * * * * /home/user/script.sh Dir1 > /tmp/logdir1.log
* * * * * /home/user/script.sh Dir2 > /tmp/logdir2.log

I had tested something like this:

FORMAT="`basename $0` $1"
OLDPID=`pgrep -f -o "${FORMAT}"`  #grab the oldest copy of the PID matching this format
echo $OLDPID:$$ 
If [ "$OLDPID" -eq "$$" ]

….

In my testing, this works great, If the oldest PID is not the same as the current one, there is another one running. however, when running from cron, the process appears twice, and so the oldest pid is NOT the current one:

user 1094 0.0 0.0 8720 944 ? Ss 12:38   0:00      \_ /bin/sh -c /home/user/script.sh Dir1 > /tmp/logdir1.log
user 1097 0.1 0.0 8856 1236 ?  S 12:38   0:00          \_ /bin/bash /home/user/script.sh Dir1

So my script fails, every single time because it sees a duplicate. is there a way to tell the pgrep to ignore the first one?

We have had some issues in the past on reading a pidfile, then seeing if that process is still running. (other processes get the same pid, and different versions of centos seem to have slightly different PS parameters)

How would you work around this?

Best Answer

Use flock as described in Overlapping jobs - locks.

Related Topic