Help with grep

Hi,

I need to monitor few services on many servers, for example ps -ef | egrep "pattern1" to "patrternN"

i would like to get only the not running services as output

ps -ef | egrep "pattern1" "patternN" | echo "Service pattern1 or patternN is not running.

Example of patterns.

pattern 1= "ABC"
patter 2= "abc xyz"
Pattern N= "abc.efg.xyz"

Thanks in advance.

As your "sample pipe" doesn't represent a sensible, runnable code (due to semantical and syntactical errors), please present any attempts from your side. What ps fields should be matched? How are the patterns transported / presented - your assignment samples won't work either?

Hi,
Here is my sample code, i would like filter the output to list only the not running/matching services...

u=u_user
srvc='ps -aef |egrep -i "ABC|abc_xyz_exe|abc_cde|abc.agent.app|ABC abc"|grep -v grep'
for host in $(cat srv.lst)
do
    ping -c2 -w3 "$host" > /dev/null
    if [ $? -eq 0 ]; then
        echo "Connecting to $host and getting the process list \n"
    ssh $u@$host $srvc
    else
    echo "$host is not available"
    fi
done

Try this adaptation of your approach:

u=u_user
srvc='ps -aef'
patterns="sleep|ABC|abc_xyz_exe|abc_cde|abc.agent.app|ABC abc"
oldIFS=$IFS
for host in $(cat srv.lst)
do
  if ping -c2 -w3 "$host" > /dev/null; then
    echo "Connecting to $host and getting the process list \n"
    pslist=$(ssh $u@$host $srvc)
    IFS=\|
    for pattern in $patterns;
    do
      case $pslist in
        (*"$pattern"*)
          :
        ;;
        (*)
          echo "Service \"$pattern\" is not running"
        ;;
      esac
    done
    IFS=$oldIFS
  else
    echo "$host is not available"
  fi
done
2 Likes

Thank you, Scrutinizer. It's working perfectly.