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 problem

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 -v grep | grep process.sh | wc -l | read a b
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.

Is something wrong?

Thanks in advance.


 
hi,

why do u think that there is only process in the process list of the name "*process.sh*" ?? print the o/p of ps -ef |grep -v grep |grep process.sh each time within the if block and see who is running the other process with a _similar_ name?

hth,
shail
 
Try it this way around, <smile>:

ps -ef | grep process.sh | grep -v grep | wc -l | read a b
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.
 
hi MikeLacey,

just want to know . what is the diff betn the two ? should give the same o/p right? u grep on process.sh first and then remove the grep process.. while the earlier was first removing the grep processes from the entire list and then grep'ing on process.sh only... other than optimization issue is it not the same??
-imho
shail.
 
In your method, you were removing all grep processes from the list, but then you were running another grep command, which effectively adds another grep to the list.

In Mikes case, he ensure all possible grep processes are in the list before he removes all instances of grep.

They are subtly different, and in rare cases scripts like this don't behave like you'd expect.

Incidentally, another way of checking, without needing the read is this. It just runs the command and then checks it's exit status ($?)
Code:
ps -ef|grep process.sh|grep -v grep >> /dev/null
[ $? = 0 ] && echo &quot;Running&quot; || echo &quot;Not Running&quot;

Greg.
 
yep -- you just have to make sure that the 'grep -v grep' bit is after all of the other grep's 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.
 
Hello,

The reason that I write &quot;wc -l|read a&quot; is because
the count return 1 because the &quot;ps&quot; is executed in the same script. My idea is that nobody execute 2 times the same script, only on time.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top