1820473 Members
3236 Online
109624 Solutions
New Discussion юеВ

checking conditions

 
SOLVED
Go to solution
Scott E Smith
Frequent Advisor

checking conditions

Here we go again. I have a script that generates a list of running processes that meet certain criteria. I need to see if any of the process names are on a list of processes. To clarify, if any of these processes are on the list then I need to take additional action. I have an if statement running in a do loop but how do I do a string comparison against a list of programs. The list is like abc,def,ghi,jkl,mno. If the process name is any of those then I need to go to the next process for comparison. Clear as mud? Thanks.
6 REPLIES 6
Bill McNAMARA_1
Honored Contributor
Solution

Re: checking conditions


function _not_running {
RUNNING=$(ps -ef|grep "$0"|grep -v grep|grep -v $$|grep -v man)
if [[ "$RUNNING" != "" ]]
then
PID=$(echo ${RUNNING} | awk {"print \$2;exit"})
echo "\nERROR: The tool is already running under pid: ${PID}\n"
echo "You must wait until it completes.\n"
exit 1
fi
}



you can modify the important bit into your loop with
for i in $(ps -ef | grep.... $LIST)
do

done

Later,
Bill
It works for me (tm)
harry d brown jr
Honored Contributor

Re: checking conditions

in ksh:

if [ ?( "abc" "def" "ghi" ) ]
then
# match
Live Free or Die
Sridhar Bhaskarla
Honored Contributor

Re: checking conditions

Scott,

Consider using XPG4 version of ps. It is a very good tool for this kind of checks.

UNIX95= ps -e -o "args" |sort >> running_procs

You can use this output to tackle your problem

Now you can use your list to compare what's in there.

STAMP=0
for PROC in `cat my_list`
do
grep $PROC running_procs > /dev/null 2>&1
if [ $? = 0 ]
then
echo "PROC is running"
STAMP=1
fi
done

if [ $STAMP = 1 ] (This means atleast one of the processes that are there in the list is running)

then
do_whatever_you_want
fi

Does this help?.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Scott E Smith
Frequent Advisor

Re: checking conditions

Question,
Harry, Is there a bin/sh format for that example?
Carlos Fernandez Riera
Honored Contributor

Re: checking conditions


ps -aef | grep -e "abd" -e "cde" -e ....

find processes

ps -aef | grep -v -e "abd" -e "cde" -e ....

Find all process except..
unsupported
harry d brown jr
Honored Contributor

Re: checking conditions

If someone doesn't answer that today, I'll try to answer that tomorrow morning.
Live Free or Die