ps -ef | grep "string1" "string2" " "string3"

Hi all,

can any one suggest me the script to grep multiple strings from ps -ef

pls correct the below script . its not working/

i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK.

bash-3.00$ ps -ef | grep blu
 lscpusr    48    42   0   Jul 22 ?         119:19 ./blusnmmp
 lscpusr 11779 11768   0   Aug 13 ?         625:01 ./bluSCCP2
 lscpusr 11620 11609   0   Aug 13 ?         621:15 ./bluSCCP1
 lscpusr 27411 27405   0   Aug 03 ?        3699:53 ./bluRBSSS
 lscpusr 11938 11927   0   Aug 13 ?         622:51 ./bluSCCP3
 lscpusr 11760 11182   0 06:04:55 pts/2       0:00 grep blu

pls check the below code is correct? pls help me on this script..

ps -ef | grep "blu" > /tmp/del.txt
if grep "./bluSCCP1" "del.txt" &&  grep "./bluRBSS" "del.txt" && grep "./bluSCP2" "del.txt" && grep "./blusnmmp" "del.txt" && grep "./bluSCCP3" "del.txt" 
then
echo "PROCESS STATUS        :  OK" >> /tmp/HC_date.txt
else
echo "PROCESS STATUS        : NOT OK" >> /tmp/HC_date.txt
fi

pls help me!!!!!

thanks

ps -ef|awk '/bluSCCP[1-3]/||/bluRBSS/||/blusnmmp/ {a++} END {print (a==5)?"OK":"NOT OK"}' >> /tmp/HC_date.txt

Hi rdcwayx,

i'm getting error like below i mentioned.

Important: if anyone the process is not running means i want to print NOT OK also.

bash-3.00$ ps -ef | awk '/bluSCCP[1-3]/||/bluRBSS/||/blusnmmp/ {a++} END {print (a==5)?"OK":"NOT OK"}'
awk: syntax error near line 1
awk: illegal statement near line 1
#!/bin/bash
ps -ef | grep "blu" > /tmp/del.txt
for S in SCCP1 RBSS SCP2 snmmp SCCP3
do
   if grep "./blu$S" del.txt
   then   echo "PROCESS STATUS        : OK"
   else   echo "PROCESS STATUS        : NOT OK"
   fi
done > /tmp/HC_date.txt

another way for inside the loop

echo "PROCESS STATUS        : "
grep "./blu$S" del.txt && echo "OK" || echo "NOT OK"

Hi frans,

Your script is working fine.. thanks.

But its showing multiple "OK" for all process. my requirement is if all process running just print only one "OK" , other wise print only one NOT OK"

PROCESS STATUS        : OK
PROCESS STATUS        :  OK
PROCESS STATUS        : OK
PROCESS STATUS        : OK

Requrement
PROCESS STATUS : OK

pls help me !!!

in Solaris, use nawk or /usr/xpg4/bin/awk.

Hi rdcwayx,

Still i'm getting some error.. can u correct it pls ?

Thanks !

bash-3.00$ /usr/xpg4/bin/awk ps -ef | awk '/bluSCCP[1-3]/||/bluRBSS/||/blusnmmp/ {a++} END {print (a==5)?"OK":"NOT OK"}'
input file "-ef"awk: syntax error near line 1
awk: illegal statement near line 1

Apologize, change the logic

#!/bin/bash
ps -ef | grep "blu" > /tmp/del.txt
STATUS="OK"
for S in SCCP1 RBSS SCP2 snmmp SCCP3
do   grep "./blu$S" del.txt || STATUS="NOT OK"
done 
echo "PROCESS STATUS        : $STATUS" > /tmp/HC_date.txt

I have checked the above code by using nawk in place of awk on solaris and it is working fine

but can you please explain the above code in detail ?

HI frans,

Thanks you.

Its working fine. can u pls explain the code?

Thanks

Br
STEVE

#!/bin/bash
ps -ef | grep "blu" > /tmp/del.txt
STATUS="OK"
for S in SCCP1 RBSS SCP2 snmmp SCCP3 # S will iterate on values
do   grep "./blu$S" del.txt || STATUS="NOT OK"
# this construction is equivalent to :
# if ! grep "./blu$S" del.txt
# then STATUS="NOT OK"
# fi
done 
echo "PROCESS STATUS        : $STATUS" > /tmp/HC_date.txt

first STATUS is set to "OK"
if one of the grep statements fails (a process isn't up), then STATUS is set to "NOT OK". We could also insert a 'break' statement to avoid the checking of other processes, the line would be written :

grep "./blu$S" del.txt || { STATUS="NOT OK"; break; }

I think you meant to type:

ps -ef | /usr/xpg4/bin/awk  '/bluSCCP[1-3]/||/bluRBSS/||/blusnmmp/ {a++} END {print (a==5)?"OK":"NOT OK"}'