I wrote a simple script, which will call other scripts or run commands on a UNIX server. my script has multiple steps/commands with some delay in between.
I usually get some email notifications after the successful execution of each step.
**My intention is to get email alerts when it is success and Failure. And it should stop at that point. It should not execute next step and should end the program there itself.
you can take the exit status of the previous command from
.
so after each command is executed check the value $?. if it contains 0 then the previous command was successfully executed else any nonzero value indicates command failed.
ls -l
if [ $? -eq 0 ]; then
echo "command successfully executed"
mailx "sub" "mail_id" < body.txt
else
echo "command failed"
mailx "sub" "mail_id" < body.txt
exit 1
fi
I just modified some of my code......anyway
i would like to run the below script, which has different steps.
if step 1 fails, then i need to get email notification and script should not execute next step.
script
#!/bin/ksh
SERVER=$(hostname)
MAILTO=abc@company.com
DATE=$(date)
ps -ef | grep java> /tmp/status1.log | mailx -s "JVM status before start" $MAILTO < /tmp/status1.log
/was/abc/bin/startManager.sh > /tmp/start.log | mailx -s "ALERT: dmgr started $SERVER at $DATE" $MAILTO < /tmp/start.log
sleep 30
/was/abc/bin/startnode.sh > /tmp/start.log | mailx -s "ALERT: node started $SERVER at $DATE" $MAILTO < /tmp/start.log
sleep 30
/was/abc/bin/startServer.sh > /tmp/start.log | mailx -s "ALERT: server started $SERVER at $DATE" $MAILTO < /tmp/start.log
ps -ef | grep java > /tmp/status2.log | mailx -s " node1 got started" $MAILTO < /tmp/status2.log
######### node 2 #############
date >> /tmp/b4strtnd2.log
ssh noide2 exec /tmp/script/mystrtscpt.sh
exit
@ Little
thanks for your input. I think i tried that option. But will do using your example. may be i did some mistake.
but if you see my script above, i would like to get the exit status and if step 1 fails, then i need to get email notification and script should not execute the next step.
This is untested, but should do what I think you're asking for:
#!/bin/ksh
SERVER=$(hostname)
MAILTO=abc@company.com
DATE=$(date)
ps -ef | grep '[j]ava' | mailx -s "JVM status before start" $MAILTO
if ! /was/abc/bin/startManager.sh > /tmp/start.log
then mailx -s "ALERT: dmgr started $SERVER at $DATE" $MAILTO < /tmp/start.log
exit 1
fi
sleep 30
if ! /was/abc/bin/startnode.sh > /tmp/start.log
then mailx -s "ALERT: node started $SERVER at $DATE" $MAILTO < /tmp/start.log
exit 2
fi
sleep 30
if ! /was/abc/bin/startServer.sh > /tmp/start.log
then mailx -s "ALERT: server started $SERVER at $DATE" $MAILTO < /tmp/start.log
exit 3
fi
ps -ef | grep "[j]ava" | mailx -s " node1 got started" $MAILTO
######### node 2 #############
date >> /tmp/b4strtnd2.log
ssh noide2 exec /tmp/script/mystrtscpt.sh
exit
Did you really intend ssh noide2 , or should it have been ssh node2 ?
Note that this will only work correctly if /was/abc/bin/startManager.sh , /was/abc/bin/startnode.sh , and /was/abc/bin/startServer.sh all return exit status 0 if they complete successfully and return a non-zero exit status if they fail. Assuming that is true this script will return exit status 1, 2, or 3, respectively if one of those scripts fails; otherwise it will return with the exit status from /tmp/script/mystrtscpt.sh on noide2.
Thanks much for your help! Just want to confirm something. I do understand that it will send me the exit status of each step. But in case if a step fails......then will it stop there itself ??
instead of executing next step?
And can we get a single attachment instead of multiple emails? and i am not receiving anything as an attachment instead am receiving all the information in the email body.
not urgent/impt........pls take your time and give some idea