Hello Forum,
I have prepared script to monitor the tomcat status. Following is the script which will monitor tomcat instance.I need little modifcation in the script. My script will grep for java,the output of grep command will analyze by if condition under for loop and will
send following echo message on the email. The echo message which will get send is as follows echo "Tomcat Application Server is down on marsvcp1" | mailx -s "tomcatserver of marsvcp1 is down" $email. I need modifcation here ,it sends very generalised message i.e tomcat is down. I would like it to send message which tomcat is going down whether it is tomcat1,2,3,4,5,6. kindly assist.
tomcat1=`ps -eaf | grep -i jdk160_05_1 | grep -v grep | wc -l`
tomcat2=`ps -eaf | grep -i jdk160_05_2 | grep -v grep | wc -l`
tomcat3=`ps -eaf | grep -i jdk160_05_3 | grep -v grep | wc -l`
tomcat4=`ps -eaf | grep -i jdk160_05_4 | grep -v grep | wc -l`
tomcat5=`ps -eaf | grep -i jdk160_05_5 | grep -v grep | wc -l`
tomcat6=`ps -eaf | grep -i jdk160_05_6 | grep -v grep | wc -l`
for tomcat in $tomcat1 $tomcat2 $tomcat3 $tomcat4 $tomcat5 $tomcat6
do
if [ $tomcat -eq 0 ]
then
echo "Tomcat Application Server is down on marsvcp1" | mailx -s "tomcatserver of marsvcp1 is down" $email
else
echo "Tomcat Application Server is running fine on marsvcp1 @ `date` ">>/root/home/root/tomcatstatus.txt
fi
done
A for loop counter may be used for that.
typeset -i cnt
cnt=1
for tomcat in $tomcat1 $tomcat2 $tomcat3 $tomcat4 $tomcat5 $tomcat6
do
if [ $tomcat -eq 0 ]
then
echo "Tomcat$cnt Application Server is down on marsvcp1" | mailx -s "tomcatserver of marsvcp1 is down" $email
else
echo "Tomcat$cnt Application Server is running fine on marsvcp1 @ `date` ">>/root/home/root/tomcatstatus.txt
fi
cnt=$cnt+1
done
Hello Anurag,
Thanks for the solution. It's working fine. When script is running from command line it is working fine. But when running from crontab it is logging below message in the logs.
Tomcat1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010
Tomcat1+1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010
Tomcat1+1+1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010
Tomcat1+1+1+1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010
Tomcat1+1+1+1+1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010
Looks like you are missing something.Can we see the modified code?
typeset -i cnt
cnt=1
tomcat1=`ps -eaf | grep -i jdk160_05_1 | grep -v grep | wc -l`
tomcat2=`ps -eaf | grep -i jdk160_05_2 | grep -v grep | wc -l`
tomcat3=`ps -eaf | grep -i jdk160_05_3 | grep -v grep | wc -l`
tomcat4=`ps -eaf | grep -i jdk160_05_4 | grep -v grep | wc -l`
tomcat5=`ps -eaf | grep -i jdk160_05_5 | grep -v grep | wc -l`
tomcat6=`ps -eaf | grep -i jdk160_05_6 | grep -v grep | wc -l`
for tomcat in $tomcat1 $tomcat2 $tomcat3 $tomcat4 $tomcat5 $tomcat6
do
if [ $tomcat -eq 0 ]
then
echo "Application Server Tomcat$cnt is down on marsvcp1" | mailx -s "tomcatserver of marsvcp1 is down" $email
else
echo "Application Server Tomcat$cnt is running fine on marsvcp1 @ `date` ">>/root/home/root/tomcatstatus.txt
fi
cnt=$cnt+1
done
but it's runnign fine from command line and not working from crontab.
Instead of following in for loop
cnt=$cnt+1
Pls try
cnt=`expr $cnt+1`
OR
cnt=$(expr $cnt+1)
or use the POSIX builtin:
i=$(( i+1 ))
or use:
$(( i+=1 ))
cnt=0
..
echo "Application Server Tomcat$((cnt+=1)) is running fine on marsvcp1
---------- Post updated at 16:35 ---------- Previous update was at 16:13 ----------
Alternatively you could do something like this:
[highlight=shell]tomcat_running() {
ps -eaf | grep -qi [j]dk160_05_$1
}
for i in 1 2 3 4 5 6
do
if tomcat_running $i; then
echo "Application Server Tomcat$i is running fine on marsvcp1 @ $(date)" >>/root/home/root/tomcatstatus.txt
else
echo "Application Server Tomcat$i is down on marsvcp1" | mailx -s "tomcatserver $i of marsvcp1 is down" $email
fi
done[/highlight]
Note: be careful if the number of instance becomes larger than 9 then the i=1 matches jdk160_05_1 as well as jdk160_05_10
or, more efficient:
[highlight=shell]PS_STRING=$(ps -eaf | grep -i [j]dk160_05_)
for i in 1 2 3 4 5 6
do
case $PS_STRING in
*"jdk160_05_$i"*)
echo "Application Server Tomcat$i is running fine on marsvcp1 @ $(date)" >>/root/home/root/tomcatstatus.txt ;;
*)
echo "Application Server Tomcat$i is down on marsvcp1" | mailx -s "tomcatserver $i of marsvcp1 is down" $email ;;
esac
done[/highlight]
Thanks, It's working fine now.