开发者

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

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 root@128.221.252.35 "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 root@128.221.252.36 "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 root@128.221.252.67 "ntpd -q -g"
echo Finished synching director-2-1-A
e开发者_Python百科cho

sleep 2

echo Changing date and time for director-2-1-B
exec ssh root@128.221.252.68 "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 root@128.221.252.69 "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 root@128.221.252.70 "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 =)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜