If condition shell script beginner

Hi all

I have the folloing process that needs checking often:

ps -ef | grep ih
 bscsrtx   206 15901  0 11:28:10 pts/6     0:00 fih -r4
 bscsrtx   218 15901  0 11:28:27 pts/6     0:01 aih
 bscsrtx 29763 15901  4 11:27:16 pts/6     0:59 rdh -prih
    root   429 27268  0 11:30:15 pts/td    0:00 grep ih
 bscsrtx   239 15901  3 11:28:41 pts/6     0:00 rih -t
 bscsrtx 29946 15901  0 11:28:04 pts/6     0:00 dih -r -s60
 bscsrtx 29770 15901  0 11:27:27 pts/6     0:20 rdh -rih
 bscsrtx   244 15901  2 11:28:47 pts/6     0:00 prih

so I wrote a little script, to check it, and at the moment my sendmail is not working, so please can you correct it if something is wrong:

#!/bin/sh

> /tmp/fr/maillist.txt
ps -ef | grep ih | grep -v ih > /tmp/fr/maillist.txt

if [ -s /tmp/fr/maillist.txt ] ; then
echo "Process is running"
else
cat /tmp/fr/maillist.txt | mailx -s "processos down" xxxi@yyy.co.zz
fi

---------- Post updated at 12:00 PM ---------- Previous update was at 11:39 AM ----------

I have made a mistake on ps command, so if I change the script like this:

#!/bin/sh

ps -ef | grep -v grep | grep ih > /dev/null

if [ $? -ne 0 ]; then
mailx -s "rating down" xxx@yyy.co.zz
fi

will it work?

Using pgrep :-

if pgrep -fl ih > /dev/null
then
        :
else
        mailx -s "Rating down" user@domain.com
fi

is there anything wrong with my code?

No, there is nothing wrong with your code.

1 Like

You can cut a grep out of your pipeline by changing:

ps -ef | grep ih | grep -v ih > /tmp/fr/maillist.txt

to:

ps -ef | grep 'h' > /tmp/fr/maillist.txt

Hi

If I use this version of the script:

#!/bin/sh

> /tmp/fr/maillist.txt
ps -ef | grep 'h' > /tmp/fr/maillist.txt
if [ -s /tmp/fr/maillist.txt ] ; then
echo "Process is running"
else
cat /tmp/fr/maillist.txt | mailx -s " rating down" xxx@yyy.co.zz
fi

the output is Process is running

But they are not because I kill them

If I use this second version:

#!/bin/sh

ps -ef | grep -v grep | grep ih > /dev/null

if [ $? -ne 1 ]; then
mailx -s "rating down" xxxx@yyyy.co.zz
fi

I got nothing:

 #./ih1.sh

Please can you help me in what could be wrong

I'm not at all sure that I understand what you're saying. Are you saying that you have a script named ./ih1.sh which you run to determine if anything is running containing the string ih after you have killed everything running that contains the string ih?

You do realize that while you're running a script named ih1 there is something running containing the string ih don't you?

Hi

ok, I have one server running HP-UX that has some processes ps -ef | grep ih ,

#ps -ef | grep ih
 bscsrtx  4940     1  4 09:25:39 ?         0:21 rdh -rih
 bscsrtx  5086     1  0 09:26:19 ?         0:27 aih
 bscsrtx  5070     1  0 09:26:01 ?         0:00 fih -r4
 bscsrtx  5099     1  0 09:26:31 ?         0:00 prih
 bscsrtx  4936     1  0 09:25:32 ?         0:57 rdh -prih
    root 16109  3723  1 10:54:32 pts/td    0:00 grep ih
 bscsrtx  5095     1  0 09:26:25 ?         0:00 rih -t
 bscsrtx  4961     1  0 09:25:53 ?         0:04 dih -r -s60

that needs to run all the time. The idea of the script is to warn us by mail when this processes are not running.
So initially I created this script:

#!/bin/sh

> /tmp/fr/maillist.txt
ps -ef | grep 'h' > /tmp/fr/maillist.txt
if [ -s /tmp/fr/maillist.txt ] ; then
echo "Process is running"
else
cat /tmp/fr/maillist.txt | mailx -s "rating down" xxxx@yyyy.co.zz
fi

which is not doing its job.

Them I created this other version of the script, which also does not work:

#!/bin/sh

ps -ef | grep -v grep | grep ih > /dev/null

if [ $? -ne 1 ]; then

mailx -s "rating down" xxxx@yyyy.co.zz
fi

How about searching explicitly for the full process/command names, and then counting the resulting lines, reacting if the number is NOK?

What are the names of these scripts?

What is the contents of /tmp/fr/maillist.txt when your 1st script is incorrectly reporting Process is running ?

How is your 2nd script not working?

Remove the > /dev/null from your 2nd script. What output does it produce when it is not working?

Hi

Sorry for this late reply, when I run my first script and the message is Process is running the contents of /tmp/fr/maillist.txt is

 #more /tmp/fr/maillist.txt
 bscsrtx  4940     1  1 09:25:39 ?         0:21 rdh -rih
 bscsrtx  5086     1  0 09:26:19 ?         0:39 aih
 bscsrtx  5070     1  0 09:26:01 ?         0:00 fih -r4
 bscsrtx  5099     1  3 09:26:31 ?         0:00 prih
 bscsrtx  4936     1  1 09:25:32 ?         0:57 rdh -prih
    root 20916  3257  0 11:33:57 pts/tc    0:00 /bin/sh ./ih.sh
 bscsrtx  5095     1  3 09:26:25 ?         0:00 rih -t
 bscsrtx  4961     1  0 09:25:53 ?         0:07 dih -r -s60
maillist.txt: END

On the second script after removing what you suggested, the output of the script is:

./ih1.sh
 bscsrtx  4940     1  2 09:25:39 ?         0:21 rdh -rih
 bscsrtx  5086     1 27 09:26:19 ?         0:41 aih
 bscsrtx  5070     1  0 09:26:01 ?         0:00 fih -r4
    root 21266  3723  1 11:37:06 pts/td    0:00 /bin/sh ./ih1.sh
 bscsrtx  5099     1  4 09:26:31 ?         0:00 prih
 bscsrtx  4936     1  3 09:25:32 ?         0:57 rdh -prih
 bscsrtx  5095     1  4 09:26:25 ?         0:00 rih -t
 bscsrtx  4961     1  0 09:25:53 ?         0:07 dih -r -s60

(Interrupt -- one more to kill letter)

You didn't answer my 1st question:

but from the output marked in red above, I will assume that they are named ih.sh and ih1.sh , respectively. I have no idea which of these 8 processes (in each case) you are trying to be sure is "alive", but as I said before, every single test you are using to determine if there is a process running that contains the string ih will detect that the script that you are using to determine that the script is running (whether or not any of the other 7 processes in the output shown above are running). If you want to be sure that no process is running with a name or arguments containing the string ih , you CAN NOT use a script with a name containing the string ih to determine that nothing is running containing that string!

1 Like

The maillist doesn't give too much evidence the processes are killed as you stated in post#6. Methinks the output "process is running" is dead-on.

Hi

I have now changed the name of my first script, and now its working fine, thank you.