Visit Our UNIX and Linux User Community


Grep multiple instances and send email.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep multiple instances and send email.
# 1  
Old 07-22-2011
Grep multiple instances and send email.

Removed

Last edited by saisneha; 07-24-2011 at 05:31 PM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-22-2011
Bug

hi,

you can try to grep with reg exp, like

Code:
#your command # | grep -c ^AppInstance

This will grep all the app instances in that status variable and then one by one you can check the status of them in the if loop.
# 3  
Old 07-22-2011
Removed

Last edited by saisneha; 07-24-2011 at 05:31 PM..
# 4  
Old 07-23-2011
MySQL

I think you want the status of instances of all/different applications... so you can take an array which stores the name of different applns whose instance's status you want to check and for each application you can run that command i mentioned earlier... it will be something like this...

Code:
application={"app1","app2"} #array with app names
#start of for loop
          #if statement with condition to check status
                       #in if condn grep will be like this ->  | grep ^${application[i]}  i.e. i th element of appln

          #end of if
#end of for

Kindly bear with the syntax... please refer to man for correct one... Smilie








Quote:
Originally Posted by saisneha
Can you please give me a example to use the loop,. I am very new to this.

I would like to grep app1, app2, xxx, yyy, 3333 etc and give the status if they are in the stop status.

Thank you,
Sai

---------- Post updated at 04:59 PM ---------- Previous update was at 10:09 AM ----------

Hi,

Can anyone help please. This code does not work for me. I neither get email not get any error.

AppInstance=(abc, def, ghi, jkl, mno, opr etc )
for i in "${AppInstance[@]}"
do
AppInstance_status='ps -ef | grep {AppInstance[i]}'
if [ -z "$AppInstance_status" ];
then
echo "App Instances are not running . $(hostname) as on $(date)"
> ~\AppInstance_status.txt
fi
done
cat ~\AppInstance_status.txt | mail -s "Alert: AppInstance status check on Production " email_address

I have the complete list of grep commands in the txt file which should run and give the output.

Thanks in advance.

Sai
# 5  
Old 07-23-2011
Please use code tags when posting code and/or in/output. It makes it much easier to read.

Try this:

Code:
logFile=~/AppInstance_status.txt #store in a variable; easier to maintain
AppInstance=(abc def ghi jkl mno opr )  #make an array
for i in "${AppInstance[@]}" ; do  #loop through the arrray
   AppInstance_status=`ps -ef | grep $i | grep -v grep`  #backticks, not single quotes; 
   if [ -z "$AppInstance_status" ] ;  then
     echo "App Instance $i is not running . $(hostname) as on $(date)"  >> $logFile  #append with >>
   fi
done

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

When you do
Code:
ps | grep string

, you have to get rid of the grep process itself, otherwise you will always get at least one line. That's what 'grep -v grep' does.
Backticks return the output of the command, big difference than single quotes, which give you the literal content of what's inside the quotes.
'>>' will append to whereas '>' will overwrite the file
UNIX based systems use forward slash as path delimiter, not backslash: ~/myfile.txt

Last edited by mirni; 07-23-2011 at 03:15 PM.. Reason: fixed commas
# 6  
Old 07-23-2011
Removed

Last edited by saisneha; 07-24-2011 at 05:32 PM..
# 7  
Old 07-23-2011
MySQL

hey
may be you can check with the logfile path... please specify the full path to logfile variable in the beginning and then run... i think it should run... Smilie

Quote:
Originally Posted by saisneha
Hi,

Thank you all your help.

At last my code works to some extent and could not read the txt file. Here is the error.

cat: cannot open /home/xxx/AppInstance_status.txt
Null message body; hope that's ok
___________________________
Here is the script I executed.

#!/bin/sh

logFile=~/AppInstance_status.txt

#store in a variable; easier to maintain

AppInstance=(xxx,aaa,ddd,ccc,xxx,cccfg)

for i in "${AppInstance[@]}" ;
do
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

cat $logFile | mail -s "Alert: AppInstance status check on Production " xxxx@xxxx.com

The AppInstance_status.txt file is under the home directory at the same location where I run the scripts.

example: /home/xxx/scripts/AppInstance_status.txt

but it is trying to read from /home/xxx/AppInstance_status.txt. Do I need to change any where the location of the file? What I want it to do is to look for that txt file and run all 40 instances from the file and give the results if any of the instance is down.

It is too complicated but I badly need help in this script.

Please help and again thanks in advance.

Sai

---------- Post updated at 11:42 AM ---------- Previous update was at 11:35 AM ----------

I got the txt file moved to one upper level and my code started working but have a question to rectify it.

I want the txt file to display only the instances that are under stop status and not all the instances. Right now, it displays whatever in the txt without executing the command.

Can anyone help please.

Previous Thread | Next Thread
Test Your Knowledge in Computers #760
Difficulty: Easy
Most modern (fast) CPUs have multiple levels of CPU caches
True or False?

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

Featured Tech Videos