Check if it a program is already running in Unix

There is more than one way to do it, the safe is probably to check if /home/lsc/OH_YES_I_AM_RUNNING exists and believe it. This is called the file.PID method and is widely used (Apache used to use it since a long long time). It needs file. It needs cleanup if you reboot your server in the middle of something (and surely you do not want to delete old pid files yourself)

Ok, often you see this :

ps -ef | grep program

There you list all processes and check the lines that contain program. So some does a vi program or anything worse (emacs?), you will get more rows than needed.

Maybe it is fine to run program with different arguments, this must be decided.

Well, take a simple test case :
x1.sh and x2.sh :
#!/bin/ksh
while :
do
date > /dev/null
done

let’s try to use ps

$ nohup ./x1.sh &
$ nohup ./x2.sh &
$ jobs
[2] + Running nohup ./x2.sh &
[1] - Running nohup ./x1.sh &
$ ps -ef | egrep 'x[12]'
u22 9240796 6226164 30 14:56:52 pts/2 0:00 /bin/ksh ./x2.sh
u22 20840608 6226164 31 14:56:48 pts/2 0:01 /bin/ksh ./x1.sh

So fine so good, I see I have one instance of each program.

Let’s try to see if the results are consistent over time :

$ n=9999;while :
do
ps -ef |
egrep 'x[12].sh'>f
if [ $(wc -l $n"
fi
done

Fri Oct 28 15:01:01 CEST 2011
u22 9240796 6226164 32 14:56:52 pts/2 0:14 /bin/ksh ./x2.sh
u22 20840608 6226164 28 14:56:48 pts/2 0:14 /bin/ksh ./x1.sh
==> 2

Fri Oct 28 15:01:08 CEST 2011
u22 9240796 6226164 50 14:56:52 pts/2 0:14 /bin/ksh ./x2.sh
==> 1

Fri Oct 28 15:01:09 CEST 2011
u22 9240796 6226164 52 14:56:52 pts/2 0:14 /bin/ksh ./x2.sh
u22 20840608 6226164 53 14:56:48 pts/2 0:15 /bin/ksh ./x1.sh
==> 2

Fri Oct 28 15:01:17 CEST 2011
u22 9240796 6226164 40 14:56:52 pts/2 0:15 /bin/ksh ./x2.sh
u22 10944520 9240796 0 15:01:17 pts/2 0:00 /bin/ksh ./x2.sh
u22 20840608 6226164 31 14:56:48 pts/2 0:16 /bin/ksh ./x1.sh
==> 3

the fact that a subshell (pid 10944520 ) of x2 appear is not a problem for me. I have much more of a problem at 15:01:08 where x1 disappeared !

Conclusion : you cannot trust ps

8 thoughts on “Check if it a program is already running in Unix

  1. Tom Kyle

    Hi Laurent,

    I don’t think your example works the way you expect it to. I don’t believe there is a guarantee that your redirection of output to file ‘f’ is complete before you read from the file again. You could force a sync before and after, or store the output in a variable. Try the following instead:

    n=9999;while :
    do
    f=`ps -ef | egrep ‘x[12].sh’`
    if [ `echo “$f”|wc -l` != $n ]
    then
    n=`echo “$f”| wc -l`
    echo
    date
    echo “$f”
    echo “==> $n”
    fi
    done

    Thanks,

    Tom

  2. Laurent Schneider

    Hi Tom,

    It it not related to a variable, i could do the ps without file. The fact is, in my example above, I had once ps without x1 in it… frightening in my opinion.

    Maybe different OS, different experiences.

  3. Tom Kyle

    Hi Laurent,

    I’m going to disagree. Your script found file ‘f’ without x1 in it. Can you reproduce with a variable not not a file?

  4. Gokhan Atil

    Hi Laurent,

    An interesting point and observation. I agree with Tom. This could be something related with synchronization of the file. I made the test (w/your code) and after 10 mins, I haven’t noticed any disappearances (@ redhat 5.2 32bit and Solaris 10 x86 64bit).

    Anyway, why do you use [ `echo “$f”|wc -l` != $n ]? As I see, you want to list the unexpected outputs. So you can use “[ `echo “$f”|wc -l` != 2 ]” or even “[ `echo “$f”|wc -l` -lt 2 ]”. Here’s the sample code:


    n=9999;while :
    do
    f=`ps -ef | egrep 'x[12].sh'`
    if [ `echo "$f"|wc -l` -lt 2 ]
    then
    n=`echo “$f”| wc -l`
    echo
    date
    echo “$f”
    echo “==> $n”
    fi
    done

    Regards

    Gokhan

  5. Laurent Schneider Post author

    AIX6

    time while ps -ef | grep -v grep |grep 'x1' ;do :;done
    oracle 12976238 30408840 61 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 62 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 63 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 33 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 34 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 35 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 37 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 39 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 40 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 41 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 42 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 43 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 44 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 45 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 46 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 47 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 48 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 49 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 50 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 51 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 53 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 54 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 55 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 56 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 57 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 58 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 59 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 30 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 31 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 33 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 34 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 35 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 36 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 37 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 38 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 39 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 40 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 41 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 42 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 43 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 44 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 45 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 47 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 48 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 49 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 50 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 51 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 52 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 53 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 54 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 27 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 28 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 31 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 32 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 33 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 35 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 36 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 36 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 38 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 39 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 40 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 41 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 43 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 44 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 45 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 46 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 48 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 48 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 50 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 52 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 53 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 54 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 27 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 28 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 29 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 31 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 32 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 33 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 35 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 36 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 37 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 38 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 39 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 40 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 41 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 42 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 43 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 44 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh
    oracle 12976238 30408840 45 09:30:03 pts/3 0:08 /bin/ksh ./x1.sh

    real 0m3.94s
    user 0m0.48s
    sys 0m0.71s

    I have no pgrep

Comments are closed.