Removed
hi,
you can try to grep with reg exp, like
#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.
Removed
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...
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.e. i th element of appln
#end of if
#end of for
Kindly bear with the syntax... please refer to man for correct one...
Please use code tags when posting code and/or in/output. It makes it much easier to read.
Try this:
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
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
Removed
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...
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:
AppInstance=(xxx,aaa,ddd,ccc,xxx,cccfg)
should be
AppInstance=(xxx aaa ddd ccc xxx cccfg)
Sorry, that was my bad in the previous post. No commas in defining an array.
No, it's not. It is writing into that text file here:
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:
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
xxx
aaa
ddd
ccc
xxx
cccfg
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.
What exactly do you mean? Could you please post your log file and elaborate?
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?
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 .
removing the info
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:
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:
SummaryHandler_status=`ps -ef | grep -c SummaryHandler`
or, which is the same,
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:
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?
if [ $SummaryHandler_status -eq 2 ] ; then ...
Because the -z tests whether the variable is empty, it is not appropriate here.
The test
if [ -z "$SummaryHandler_status" ] ; then
Will never pass, since SummaryHandler_status is never empty. It contains a number. The test
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:
SummaryHandler
Validator
VEEIntervalMonitorMessageProcessorApp
...
Then, you can loop through it like this:
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:
SummaryHandler 2
Validator 2
RTFFramer 3
...
Then you can read this in and test against it:
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:
#!/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
chmod 754 checkApps.sh
Create the text file Apps.txt as I showed above and run it:
./checkApps.sh
And let me know what happened.
Thank you for your help. It works now.
Sai
HI,
Thank you for your great help. Can you please remove all the info which is my personal from the thread since I do not want anyone to look at this and it has my personal information.
Thank you again for everything.
Sai
Glad it worked.
I removed your email, which was the only sensitive info. But why did you remove all your posts?
The idea behind the forum is that others may benefit, when they encounter similar problems and come across a post (most people come across a post like this from a search engine, when they try to solve their issues.) Now the thread doesn't make much sense, since the original problem statement is missing. There was nothing personal or confidential in the thread, apart from your email address, which even that I didn't consider confidential, since you posted it (and many members make their email address available).
Open-source and sharing is the means of fastest advancement. That's what unix community is about. I committed quite some time to solving your issues with the hope that you will learn, and perhaps, teach others later on. I kindly ask you to commit a few moments to reviewing your concepts of sharing. Thanks.