9

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 =)

5
  • man exec will explain why that happens. get rid of the exec and you should be golden. OTOH this looks like some serious abuse of ntpd here! Commented Aug 25, 2011 at 16:38
  • you have to tell us why you need to exec each ssh. :g/exec/s/exec// (remove all the execs and it should work). Good luck. Commented Aug 25, 2011 at 16:39
  • @fvu: man sh is likely a better way to find out what the exec built-in does. Commented Aug 25, 2011 at 17:50
  • @Jens have you tried it? :-) man exec goes to the BASH_BUILTINS manpage, where exec is described - that way even someone who doesn't know exec is a builtin function of bash will get to the correct info. Commented Aug 25, 2011 at 19:01
  • @fvu: Yes, I've tried it, albeit on FreeBSD, where man exec brings up a man page with 4 columns, "Command", "External", "csh(1)", "sh(1)", giving Yes/No for each of the builtins (see freebsd.org/cgi/…) This shows the necessity of weasel words like "is likely" :-) Commented Aug 25, 2011 at 19:23

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

Comments

11

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.