-2

Struggling with this...

for i in `cat services.txt`
do
if ! grep -q $i; then
echo " $i Is NOT Running"
else
echo " Checking $i on `hostname`..."
ps aux | grep -i $i | awk '{print $1, $11}'| cut -d' ' -f1-2| sort
echo -e " "
sleep 4
fi
done

The block just hangs - Ive not been able to capture the success/failure of grep

If a string in services.txt is NOT found ... the script hangs... Id like for grep to skip it if not found

services.txt contain just single words

thanks!

2 Answers2

0

The reason your script hangs is beacuse the command grep -q $i is waiting for an input. You can try running that command separately in a shell and verify that it prompts for an input.

Changing your command to ps aux | grep -i $i in the if statement should fix your issue.

NOTE: ps aux | grep -i $i lists grep also as one of the process. Make sure you exclude that process by piping it to another grep ps aux | grep -i $i | grep -v 'grep'

  • Thanks Vijay - I realized that later but this was still challanging - appreciate your time and input. I will post the working code below – Leadorfollow Jun 05 '20 at 16:20
0

here is the working code

checkservices() {
cat >$HOME/services.txt <<EOF
ganglia
hbase
hdfs
hive
hue
livy
mapred
test-missing-service
mysql
oozie
presto
spark
yarn
zeppelin
EOF

for i in `cat $HOME/services.txt`
do 
if `ps -ef  | grep ^$i | grep -v grep >/dev/null`
then 
i=$(echo "$i" | awk '{print toupper($0)}')
echo "$i -- is running on `hostname`"
echo ""
sleep 2
else 
i=$(echo "$i" | awk '{print tolower($0)}')
echo "$i -- IS NOT running on `hostname` error"
echo ""
fi
done
}
  • Run that through http://shellcheck.net and it'll tell you about some of the problems with it. – Ed Morton Jun 06 '20 at 00:30
  • 1
    Ed, thank you so much, thats a great tool and help. Im old(er) school and never was interested in those new methods because mine work. But this explains how and why to do it. . Much appreciated – Leadorfollow Jun 08 '20 at 13:45