Operating System - HP-UX
1833151 Members
3761 Online
110051 Solutions
New Discussion

check for process in shell script

 
Printaporn_1
Esteemed Contributor

check for process in shell script

Hi folk,

I want to check for a running process in my shell script by if this process already running the script will exit
I use
WFRUN=`ps -ef|grep wfrun|grep -v grep|head -
to check for process wfrun does it running ,

when run in prompt it will not return any strings if wfrun was not running but when I use in shell why it return PID of grep wfrun.
that mean it always think that process is running.

please help on this


enjoy any little thing in my life
5 REPLIES 5
Printaporn_1
Esteemed Contributor

Re: check for process in shell script

sorry the above question is not complete anyway I found solution by using

WFRUN=`UNIX95= ps -f -C wfrun | grep cron | awk '{print $2}'`
enjoy any little thing in my life
Steve Post
Trusted Contributor

Re: check for process in shell script

What is the name of the program checking for wfrun? Is it something like check_wfrun? If so, your ps -ef command will find a process id for the check_wfrun.
Run "ps -ef | grep wfrun" so you can see the output.

Once I had outbound_edi.rc checking for a process of outbound_edi.sh. But, because I searched for just outbound_edi, I got BOTH outbound_edi.rc and outbound_edi.sh processes.
Wodisch
Honored Contributor

Re: check for process in shell script

Hello Printaporn,

you should do the exclusion of "grep" first, for you never
know the order of "grep" and the process you are
looking for:
ps -ef | grep -v grep | grep YOUR-PROCESS

Then it should be working.

HTH,
Wodisch
boley janowski
Trusted Contributor

Re: check for process in shell script

it looks like you found your resolution, the UNIX95 has some pretty cool stuff with it. Steve also made a good point. Anyway thanks for the command.
Klaus Crusius
Trusted Contributor

Re: check for process in shell script

Hi Printaporn,

we had a similar problem using ps | grep .... .
It showed up, that there is no predictable order of execution of fork and exec system calls in the processes started for the pipe. So, depending on the processor performance and shell version, the ps -ef command can report one additional process for each process in the pipe with the same name as the originating script.

To overcome this problem:

WFRUN1=`UNIX95= ps -f -C wfrun `
WFRUN=`echo "$WFRUN1"| grep cron | awk '{print $2}'`

Klaus
There is a live before death!