Shell script with mutiple steps/commands, on UNIX servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script with mutiple steps/commands, on UNIX servers
# 1  
Old 07-30-2013
Shell script with mutiple steps/commands, on UNIX servers

Hi,

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.

any idea pls.

Thanks,
# 2  
Old 07-30-2013
Why don't you show us what you have and we'll show you how to check for errors.

What shell are you using?
# 3  
Old 07-31-2013
you can take the exit status of the previous command from
Quote:
$?
.
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.

Code:
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

# 4  
Old 07-31-2013
@ Don Cragun

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
Code:
#!/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.

Thanks.........appreciate your help

Last edited by Scott; 07-31-2013 at 06:13 PM.. Reason: Code tags, please...
# 5  
Old 08-01-2013
This is untested, but should do what I think you're asking for:
Code:
#!/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.

Last edited by Don Cragun; 08-01-2013 at 07:42 AM.. Reason: Add missing <newline>
# 6  
Old 08-01-2013
@Don Cragun

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

Thank you

Last edited by System Admin 77; 08-01-2013 at 02:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to ignore mutiple strings when using shell script?

Hi All, I am trying to use below syntax to find ignore multiple locations while searching for a file. find / -name "$serviceitem" ! -size 0 2>&1 |egrep -v "tmp|docker|WinSxS|Permission|HISTORY|alternatives|bearer11ssl|manifest" I tried to assign all the ignore strings to one variable... (2 Replies)
Discussion started by: sravani25
2 Replies

2. Shell Programming and Scripting

Shell script to set user password to never expire in UNIX servers

Hi, I have a requirement where in i need to write a shell script to set users password to never expire. I know the command which is used to perform the same .. which is chage command. But, could not figure out how to do the same in shell script. Could you please help me with the shell... (3 Replies)
Discussion started by: suren424
3 Replies

3. Shell Programming and Scripting

Running set of commands in remote servers in shell script

Hi Wishing to all. I am very new joined in an organization as a unix system administrator. I need a help in preparing a script for a report. i have a file contains all of the linux/ubuntu servers line by line around 140 servers. vi servers.txt nh01 nh02 nh03 bh01 bh04 - - :wq (3 Replies)
Discussion started by: kumaraswamy
3 Replies

4. UNIX for Dummies Questions & Answers

awk script combining mutiple commands

Hi, I am pretty new to the unix community and have encountered a problem that I am trying to solve. I have 2 files one of which is called passwd file that looks like the following Sample Output daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh ... (1 Reply)
Discussion started by: raven905
1 Replies

5. Shell Programming and Scripting

Need help embedding Unix commands in a shell script

Hi Folks, I have a text file which may or may not have any data. I would like to email the file, via a Korn shell script, if the file is not empty. I am fiddling around with the wc -l command, but no luck so far. The pseudo code is as follows count=`wc -l test.txt` if cat test.txt... (4 Replies)
Discussion started by: rogers42
4 Replies

6. Shell Programming and Scripting

to create a phone book using shell script and unix commands

can you help me to create a phone book with add, delete, modify with first name, last name, middle name, phone no(multiple ph no), address, email address, notes or comments to store about the contact and groups that hold for the contact.. i am new to this linux environment. please guide me. ... (1 Reply)
Discussion started by: monster11209
1 Replies

7. Shell Programming and Scripting

How to incorporate mutiple commands in Autosys commandline on UNIX

Hi, I am having a problem running multiple commands in a autosys command line. For example I would like to run a perl script first and then add sleep command after that in the same autosys job like below insert_job: xxxxxxxxx command: `perlscript.pl ; sleep 180` Perlscript.pl... (0 Replies)
Discussion started by: waavman
0 Replies

8. Shell Programming and Scripting

Unix commands failing inside the shell script

When my script deals with large input files like 22Gb or 18 GB the basic commands like sort or join fails when run from inside the shell scripts. Can there be any specific reason for this? For e.g. sort -u -t "," -k1,1 a.csv > a.csv.uniq" sort -u -t "," -k1,1 b.csv > b.csv.uniq" The... (3 Replies)
Discussion started by: esha
3 Replies

9. Shell Programming and Scripting

unix shell script which inserts some records into a file located in remote servers...

All, I need to write an unix shell script which inserts some records into a file located in remote servers. * Get the input from the user and insert according the first row. It should be in ascending order. 123451,XA,ABA 123452,XB,ABB 123453,XC,ABC 123455,XE,ABE 123456,XF,ABF 123458,XG,ABG... (2 Replies)
Discussion started by: techychap
2 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question

Featured Tech Videos