PS finds a ghost?

Hello,

I have problems executing a script in ksh with this script named
process.sh:

ps -ef | grep process.sh | grep -v grep | wc -l | read a
if [ $a -gt 1 ]
then
 echo "The script is running" 
exit 0
fi

The problem is that when I execute the script, sometimes it shows the
message "The script is running", when I think it's impossible.
The reason that I write "wc -l|read a" is that
the count returns 1 because the "ps" is executed in the same script.
My idea is that nobody execute 2 times the same script, only on time.

Is something wrong?

Thanks in advance.

added code tags for readability --oombera

How about:

#!/bin/ksh

ps -ef | grep "[p]rocess.sh" 1> /dev/null 2>&1
if [$? -eq 0]
then
	echo "script is running"
	exit 0
fi
exit 1

added code tags for readability --oombera

The script is always running 1 time, I must insert the code in the same script to control if someone will execute the script again.

Ok, I understand now. Why don't you make the script test for a lockfile when it starts?

if [ -a /tmp/process.lock ]; then
exit
fi

touch /tmp/process.lock
# rest of script
rm /tmp/process.lock

Your idea is good, but I need to implement it in a lot of scripts (77 aprox), a lot of them running at the same time.

What I don't want is to use 77 different names for each temporary file.

Ok, third time's a charm...

What about:

#!/bin/ksh
NUM=`ps -ef | grep "[p]rocess.sh" | wc -l
if [ $NUM -gt 1 ] 
then 
            echo "The script is running" 
            exit 0 
fi

added code tags for readability --oombera

Script is perfect.

But it lost closing quote. isn't it?

The script you wrote is very similar to mine, isnt'it?

Yep, I lost a closing quote. Typo.

It is similar to the original script you posted, except that instead of piping the line count to "read", I just assign it directly to a variable -- this is a cleaner and more direct method of doing it. I also eliminated the
"grep -v grep" with a little shell trickery -- the [s]quare brackets on the first letter of the script name. Its a personal preference, but I like this method better.

Thanks for your reply, but I have tested your code and my code 100 times (with a "for" writing to a log) and your code has failed 74 times and my code 63.

My question is why fails it?

Thanks in advance.

I recommend saving the output of your ps command (without the 'wc -l') to a log file so that you can see why it is failing.