Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/ Dir1 > /tmp/logdir1.log
* * * * * /home/user/ 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/ Dir1 > /tmp/logdir1.log
user 1097 0.1 0.0 8856 1236 ?  S 12:38   0:00          \_ /bin/bash /home/user/ 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?

share|improve this question
Please see Process Management to see if it has anything helpful. – Dennis Williamson Aug 23 '13 at 21:47

Use flock as described in Overlapping jobs - locks.

share|improve this answer

Do something like this:



if [ -f ${pidfile} ]; then # is there already a  pid file?
  oldpid=$(cat ${pidfile})
  ps -p ${oldpid} &> /dev/null # is the process still running?
  if [ $? -ne 0 ]; then
    rm ${pidfile} # pid file is stale, remove it
    echo "Old process still running"
    exit 1

echo $$ > ${pidfile}


rm ${pidfile}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.