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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script that echo 0 if a process(parameter) is not running.

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I trying to make a script that echo 1, if the process that is given to it, is not running. - just a ps command
ps -ef|grep "parameter"
The problem is that I need to give it several parameters
ps -ef|grep "parameter1" |grep "parameter2"


this ex. has 2 parameters
ProcessExists.ksh "test" "test 2" - the ps command should then be something like this
ps -ef|grep "test" |grep "test 2"


I've tryede to make a script that runs throu the parameters - but I can't get them to echo out the parameter.



#!/bin/ksh

i=0
Item=1

while [ $i -lt $# ]
do

echo $Item " Item"

parm="$"$Item;echo `echo $parm`
echo $$Item

(( Item=$Item+1 ))
i=$((1+i))

done
 
Something like this ?
#!/bin/ksh
: ProcessExists.ksh process1 [process2 ...]
[ $# -ge 1 ] || exit 1
nbSearch=$#
search="$1"; shift
for i; do search="${search}|$i"; done
[ $(ps -fe | grep -cE "$search") -lt $nbSearch ] && echo 1

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I'm not quite sure I understand what you do.

you go throu the parameters and putting | between them - ending up with something looking like this.
ProcessExists.ksh process1 process2
process1|process2
Then you do a ps -ef|grep -cE "process1|process2"

I can't realy figure out what this does, but it does not do tha same as
ps -ef|grep process1|grep process2
(I want to find the processes that contains process1 and process2)

Another ting is that the script should handel spaces
ex
ProcessExists.ksh "process 1" "process2"
should only pick the first of these processes - not the last one
root 479 1 0 Jan 9 ? 0:00 process 1 process2
root 480 479 0 Jan 9 ? 29:52 process1 process2

like with
ps -ef|grep "process 1" |grep "process2"


Thanks
 
Sorry for the misunderstanding.
The above script should echo 0 if process not found:
[ $# -ge 1 ] || exit 1
search="$1"; shift
for i; do search="${search}|$i"; done
ps -fe | awk -v search="$search" '
BEGIN{n=split(search,s,"|")}
{for(i=1;i<=n;++n)if($0!~s)next;exit}
END{print 0}'

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
This works on Sun/ksh. It returns 0/1 rather than echo but is easy enough to change...

#! /usr/bin/ksh

SEARCH=`ps -ef | grep -v "ps -ef" | grep -v "${0} ${1}` >> /dev/null

while [ $# -gt 0 ]
do
echo ${SEARCH} | grep "${1}" >> /dev/null
if [ $? -ne 0 ]
then
exit 1
fi
echo ${1}
SEARCH=`echo ${SEARCH} | grep "${1}"`
shift
done
exit 0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top