Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one create a script to execute multiple "exec" commands sequentially?

Tags:

shell

I am very new to Linux Shell Scripting and was wondering if anyone could help me with the following.

I created a script to synch time with my linux machine but only one exec command seems to complete

#!/bin/bash
#Director SMS Synch Time Script

echo The current date and time is:
date
echo

echo Synching GTS Cluster 1 directors with SMS.
echo
echo Changing date and time for director-1-1-A
exec ssh [email protected] "ntpd -q -g"
echo Finished synching director-1-1-A
echo

sleep 2

echo Changing date and time for director-1-1-B
exec ssh [email protected] "ntp -q -g"
echo Finished synching director-1-1-B
echo

sleep 2

echo Finished Synching GTS Cluster 1 directors with SMS.
sleep 2
echo
echo Synching SVT Cluster 2 directors with SMS.
echo
echo Changing date and time for director-2-1-A
exec ssh [email protected] "ntpd -q -g"
echo Finished synching director-2-1-A
echo

sleep 2

echo Changing date and time for director-2-1-B
exec ssh [email protected] "ntpd -q -g"
echo Finished synching director-2-1-B
echo

sleep 2

echo Changing date and time for director-2-2-A
exec ssh [email protected] "ntpd -q -g"
echo Finished synching director-2-2-A
echo

sleep 2

echo Changing date and time for director-2-2-B
exec ssh [email protected] "ntpd -q -g"
echo Finished synching director-2-2-B

sleep 2

echo

echo
echo Finished Synching SVT Cluster 2 directors with SMS.

The script only seems to complete after the first exec command.

Thu Aug 25 12:40:44 EDT 2011

Synching GTS Cluster 1 directors with SMS.

Changing date and time for director-1-1-A

Any help would be greatly appreciated =)

like image 431
user912558 Avatar asked Aug 25 '11 16:08

user912558


People also ask

How do you write a script with multiple commands?

We can start multiple commands as a single job through three steps: Combining the commands – We can use “;“, “&&“, or “||“ to concatenate our commands, depending on the requirement of conditional logic, for example: cmd1; cmd2 && cmd3 || cmd4.

Does a shell script execute commands sequentially?

To answer your title question: Yes, commands in a shell script are executed synchronously in sequence, so the shell is blocked while your Python script is running.


2 Answers

The whole point of exec is to replace the current process. In shell scripts this means the shell is replaced and nothing after the exec is executed any more. My wild-assed guess is: maybe you want to background the commands with & instead (ssh ... &)?

If however you just want to run the sshs in sequence, each time waiting until it has completed, just remove the 'exec' words. There's no need to express "I want to run this_command" with exec. Just this_command will do the trick.

Oh, and make this a #!/bin/sh script; there are no bashism or linuxism in your script. It is good practice to avoid bashisms if you can. This way your script could be run unmodified if your boss decides to switch to, say, FreeBSD.

like image 167
Jens Avatar answered Dec 09 '22 08:12

Jens


you can to run all commands but the last in background, and the last with exec :

for example if you have 4 commands :

#!/bin/bash

command1 &
command2 &
command3 &

exec command4

Processes tree before exec is executed :

bash                         < your terminal
  |
  +----bash                  < the script
         |
         +------command1
         |
         +------command2
         |
         +------command3

Processes tree after exec is executed :

bash                         < your terminal
  |
  +------command4
            |
            +------command1
            |
            +------command2
            |
            +------command3

As you see, the ownership of the first three commands is transferred to command4 when the bash process for the script is replaced by command4

Note:

If command4 exits before the other commands, processes tree becomes:

init                         < unix init process ( PID 1 )
  |
  +------command1
  |
  +------command2
  |
  +------command3

although ownership should logically have been transferred to the bash terminal process ? Unix mysteries...

like image 30
earlgrey Avatar answered Dec 09 '22 08:12

earlgrey