Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PS finds a ghost

Status
Not open for further replies.

jlguirao

Programmer
Nov 15, 2000
7
ES
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.







 
This came up before didn't it.... I'm puzzled, looks as if it should work ok. Any ideas anybody? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Looks fine to me .. Only thing I can suggest is that its working fine and another process with the string process.sh in it, is running at the same time..
Only thing I can think of is change your ps line to

ps -eaf | grep process.sh | tee logfile | grep -v grep | wc -l | read a

That way you'll get a listing of what processes are running during execution in the logfile

hope that helps
 
Try

a=$(ps -ef | grep "[p]rocess.sh" | wc -l)

or similar.

By putting [] around the p, you hide the grep process from the grep process (sorry about the wording) but still find the running process you seek.

If this doesn't do help, the tee tip above is a good idea to isolate the list of running processes. Maybe someone else periodically runs a process with the same name.
 
Are you sure the process.sh has finished processing before you execute the script again. You said it only happens sometimes. There is nothing wrong with the script. The process probably has not died from the previous run.
 
Throw in a grep -v $$ somewhere in there to exclude the PID of the script you're running. I sometimes see ps return two lines for one process. Darned if I know why.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top