usage of ps -ef

Hi,
I can have only one instance of my script running at any point of time. So I have :
SCRIPT_NAME=$0
if [ `ps -ef | grep " ${SCRIPT_NAME}" | grep -v "grep" | wc -l` -gt 1 ]; then
echo "The process $0 is already running. Aborting the current instance of the job $0"
exit 1

fi

And in the last line of the script i had given exit 0

At times, though the script is not running, i get an error "The process is already running ..." and my script get aborted. So wat can be done for this ?

My requirement is I will scheudle this scirpt in autosys and will be executed every half an hour. And only one instance of the script should be executed at any point of time.

Can any one please help me ??

thanks in advance.

Echo $SCRIPT_NAME and compare it what ps -ef give as output. Maybe they are slightly different and so grep can't find it that way. Maybe you got to parse or escape the leading ./ when you call it.

I tired that too..
That is giving correctly..

wat else can be done ...

Your script works - I added an infinite loop so I have that script constantly running in the background. Started it with nohup & in the background and tried to start that script again:

root@isau02:/data/tmp/testfeld> ./loop.ksh
exit code: 0
script name: ./loop.ksh
The process ./loop.ksh is already running. Aborting the current instance of the job ./loop.ksh
root@isau02:/data/tmp/testfeld> ps -ef| grep -i loo
root     29647 32139  0 09:25 pts/0    00:00:00 /usr/bin/ksh ./loop.ksh
root     29685 29659  0 09:26 pts/1    00:00:00 grep -i loo


root@isau02:/data/tmp/testfeld> cat loo*
#!/usr/bin/ksh

typeset -i A=0
SCRIPT_NAME=$0

if [ `ps -ef | grep " ${SCRIPT_NAME}" | grep -v "grep" | wc -l` -gt 1 ]; then
        echo "exit code: $?"
        echo "script name: ${SCRIPT_NAME}"
        echo "The process $0 is already running. Aborting the current instance of the job $0"
        exit 1
fi

while (( $A != 1 )); do
        sleep 1
done

exit 0