Grep multiple instances and send email.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep multiple instances and send email.
# 8  
Old 07-23-2011
Please use code tags when posting code and/or in/output. It makes it much easier to read.


You still have some syntax errors there:
Code:
AppInstance=(xxx,aaa,ddd,ccc,xxx,cccfg)

should be
Code:
AppInstance=(xxx aaa ddd ccc xxx cccfg)

Sorry, that was my bad in the previous post. No commas in defining an array.

Quote:
but it is trying to read from /home/xxx/AppInstance_status.txt.
No, it's not. It is writing into that text file here:
Code:
echo "App Instance $i is not running . $(hostname) as on $(date)" >> $logFile

If you want to read the instances from file, instead from an array, just change the for loop like this:
Code:
logFile=~/AppInstance_status.txt 
while read i ; do  #loop through the file
   AppInstance_status=`ps -ef | grep $i | grep -v grep`
   if [ -z "$AppInstance_status" ] ;  then
     echo "App Instance $i is not running . $(hostname) as on $(date)"  >> $logFile
   fi
done < AppInstances.lst

cat $logFile | mail -s "Alert: AppInstance status check on Production " email_address

where AppInstances.lst is the file with one entry on a line. So for your previous example, it would be
Code:
xxx
aaa
ddd
ccc
xxx
cccfg

# 9  
Old 07-23-2011
Hi,

I could see that it is displaying all the info from the txt file. What I want it to do is, run the commands from the txt file and display in the message only the instances that are not running as required.
# 10  
Old 07-23-2011
Quote:
I could see that it is displaying all the info from the txt file
What exactly do you mean? Could you please post your log file and elaborate?

Quote:
run the commands from the txt file
Well this is new. What commands? Which file has commands? Why do you want to run the commands from file as opposed from a script, and just read the parameters of the command from a file, as suggested above?
# 11  
Old 07-23-2011
Thank you so much for your support but I really am not getting what I needed.

I basically want it to read from txt file and run the ps -ef grep commands fro 40 apps and give the result in the body of the message in the email alert which ever app is down with the name of the app instance.

When I tried < AppInstance.lst , it says

no such file or directory which I mentioned in the beginning .
# 12  
Old 07-23-2011
log file

removing the info

Last edited by saisneha; 07-24-2011 at 01:23 PM.. Reason: I want to hide it.
# 13  
Old 07-24-2011
OK. Let's call things the right names, because otherwise confusion arises:
The file you posted, AppInstance_status.txt, is not a log file. It is a script (see the '#!/bin/sh' in the first line). It is probably executable and can be run. Most people would give it a different suffix (like .sh), instead of .txt but that's not the most important thing. But it is not gonna do what you want, because there are errors there.

I want to believe you wish to understand this script, so I'm gonna explain it in more detail and point out the errors.
It has multiple sections, that all do the same thing, but more on that later. Let's look at one of the sections:
Code:
SummaryHandler_status='ps -ef | grep -c SummaryHandler'
  if [ -z "$SummaryHandler_status -ne 2" ];
  then 
    echo "There should be 2 instances of SummaryHandler app running but only 1 has been detected. 
             Please take necessary action to start the missing instance. $(hostname) as on $(date)" |
mail -s "Alert: SummaryHandler is not running" email@address
fi

As I posted before, there is a dramatic difference between single quotes and backticks. Variable SummaryHandler_status is gonna contain the string 'ps -ef | grep -c SummaryHandler' literal, because that's what single quotes do. What you want is the backticks (that's the key to the left of '1' on most keyboards), which give you the output of the command in the backticks:
Code:
SummaryHandler_status=`ps -ef | grep -c SummaryHandler`

or, which is the same,
Code:
SummaryHandler_status=$(ps -ef | grep -c SummaryHandler)

'ps -ef' will print the running processes, and 'grep -c' will count how many occurrences of "SummaryHandler" are present.
So now, SummaryHandler_status will contain an integer.
Just put an echo there to see the difference:
Code:
SummaryHandler_status='ps -ef | grep -c SummaryHandler'
echo quotes: $SummaryHandler_status
SummaryHandler_status=`ps -ef | grep -c SummaryHandler`
echo backticks: $SummaryHandler_status

Now you want to test, whether the integer $SummaryHandler_status is 2. This is because if you do 'ps -ef | grep -c SummaryHandler', you'll see at least one line with 'SummaryHandler' in it: the grep process itself. So you want to see 2 lines, one the grep process, and one the SummaryHandler process. The count can also be more than 2, if e.g. there is more than one SummaryHandler process running.

Let's test: is it 2?
Code:
if [ $SummaryHandler_status -eq 2 ] ; then ...

Because the -z tests whether the variable is empty, it is not appropriate here.
The test
Code:
if [ -z "$SummaryHandler_status" ] ; then

Will never pass, since SummaryHandler_status is never empty. It contains a number. The test
Code:
if [ -z "$SummaryHandler_status -ne 2" ] ; then

will never ever pass, because apart from the value of SummaryHandler_status it has the string "-ne 2", and thus is certainly not empty.

OK, now instead of repeating the section for each app, you could store the names of the apps in a text file and in the script do a loop as I suggested before:
If you make a text file named Apps.txt, and put the names of the apps in, one per line:
Code:
SummaryHandler
Validator
VEEIntervalMonitorMessageProcessorApp
...

Then, you can loop through it like this:
Code:
while read app ; do 
  status=`ps -ef | grep -c $app`
  if [ $status -ne 3 ] ; then 
    echo "There should be 2 instances of $app app running but $(($status - 1)) has been detected. 
              Please take necessary action to start the missing instance. $(hostname) as on $(date)" |
    mail -s "Alert: $app is not running" email@address
  fi   
done < Apps.txt

However, I noticed that not all the apps should have one instance running, e.g. RTFFramer should have 3 running. So, you could change your text file Apps.txt to store the number of instances that should be running. Like this:
Code:
SummaryHandler 2 
Validator 2
RTFFramer 3
...

Then you can read this in and test against it:
Code:
while read app cnt ; do 
  status=`ps -ef | grep -v grep | grep -c $app`
  if [ $status -ne $cnt ] ; then 
    echo "There should be $cnt instances of $app app running but $status has been detected. 
            Please take necessary action to start the missing instance. $(hostname) as on $(date)" |
    mail -s "Alert: $app is not running" email@address
  fi   
done < Apps.txt

I inserted 'grep -v grep' there, to correct for the false extra process, so that I don't have to substract one from $status.

Now that should be all you need. Make a new file called checkApps.sh and put this in there:
Code:
#!/bin/sh

## check the number of running instances of apps against the expected counts
input=Apps.txt
log=AppsErr.log
: > $log   #truncate the file if exists

if [ ! -f $input ] ; then 
   echo "I need file $input to read the apps names and expected counts from"
   exit 1
fi

while read app cnt ; do 
  status=`ps -ef | grep -v grep | grep -c $app`
  if [ $status -ne $cnt ] ; then 
    echo "There should be $cnt instances of $app app running but $status has been detected. 
             Please take necessary action to start the missing instance. $(hostname) as on $(date)" >> $log
    sendDaMail=1  #flag that error in counts has been noticed
  fi   
done < $input

if [ _$sendDaMail = _1 ] ; then  #send mail only if error was found
  mail -s "Alert: some Apps not running in correct count" email@address < $log
fi

make this script executable with
Code:
chmod 754 checkApps.sh

Create the text file Apps.txt as I showed above and run it:
Code:
./checkApps.sh

And let me know what happened.

Last edited by mirni; 07-24-2011 at 07:16 PM.. Reason: formatting
# 14  
Old 07-24-2011
Thank you for your help. It works now.

Sai
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop multiple directory, find a file and send email

Hello ALL, need a BASH script who find file and send email with attachment. I have 50 folders without sub directories in each generated files of different sizes but with a similar name Rp01.txt Rp02.txt Rp03.txt ...etc. Each directors bound by mail group, I need a script that goes as... (1 Reply)
Discussion started by: penchev
1 Replies

2. Shell Programming and Scripting

How do I use grep to pull incremental data and send to multiple files?

Hi Everyone, Im currently using the below code to pull data from a large CSV file and put it into smaller files with just the data associated with the number that I "grep". grep 'M053' test.csv > test053.csv Is there a way that I can use grep to run through my file like the example below... (6 Replies)
Discussion started by: TheStruggle
6 Replies

3. Shell Programming and Scripting

How to send email with multiple attachments ?

Hello , I am trying to send an email with two attachments . I have tried all previous suggestion in this forum but none worked. I could send one attachment in an email by uuencode $file "$file" | mailx -m -s "File" xxx@xx.com but unable to send multiple attachments . I have tried ... (8 Replies)
Discussion started by: RaviTej
8 Replies

4. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies

5. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

6. Shell Programming and Scripting

Grep with multiple instances of same pattern

Hi, This is my text file I'm trying to Grep. Apple Location Greenland Rdsds dsds fdfd ddsads http Received Return Immediately Received End My Grep command: grep only--matching 'Location.*Received' Because the keyword Received appears twice, the Grep command will stop at the last... (3 Replies)
Discussion started by: spywarebox
3 Replies

7. Shell Programming and Scripting

Grep with multiple instances of same pattern

Hi, This is my text file I'm trying to Grep. Apple Location Greenland Rdsds dsds fdfd ddsads http Received Return Immediately Received End My Grep command: grep only--matching 'Location.*Received' e. Because the keyword Received appears twice, the Grep command will stop at the last... (0 Replies)
Discussion started by: spywarebox
0 Replies

8. UNIX for Dummies Questions & Answers

to send email to multiple users

hi, i'm pretty new to this unix. i've been asked to create a shell script which will pick up the email id from a text file(stored in same machine, same directory) searches for that id in another file in which a product name( a one line text) is mentioned against it. then it should send a mail... (0 Replies)
Discussion started by: vishwas.shenoy
0 Replies

9. Shell Programming and Scripting

Using mailx to send email to multiple users.

Hi, I am using the mailx command to send email to multple users. The command works fine when i am sending mail to a single user but when i insert multiple email ids inside the quote it does not work. All the email ids are coming from a property file.Please have a lookt at the property file and... (4 Replies)
Discussion started by: priyaksingh
4 Replies

10. UNIX for Advanced & Expert Users

Unable to send eMail from a UNIX-Host ( using mailx ) to a Outlook-email-addres(Win)

Hi A) I am able to send eMail using mailx from a UNIX ( solaris 8 ) host to my Outlook-email-ID : FName.Surname@Citigroup.com ( This is NOT my actual -eMail-ID). But in Outlook the "From :" eMail address is displayed as " usr1@unix-host1.unregistered.email.citicorp.com " .i.e the words... (2 Replies)
Discussion started by: Vetrivela
2 Replies
Login or Register to Ask a Question