Ssh – reboot multiple machines remotely

bashssh

I need to reboot a number of machines remotely. Normally I just issue

for host in <hostlist>;do ssh ${host} 'sudo shutdown -r now';done

But I want give the users some time before the restart. However they ssh session won’t disconnect even if I use:

ssh -f 'sudo shutdown -r +5 &;disown'

I get an error message:

bash: -c: line 0: syntax error near unexpected token ;' bash: -c: line 0:sudo shutdown -r +5 &;disown'

Any suggestions?

Best Answer

As stated by the error message, you have a syntax error in your command. It is due to the slightly surprising fact that & isn't part of a shell command but rather a separator between commands (like ; is). Though both are command separators & has an additional effect on the command on its left hand side. This certainly also confuse me sometimes, and I frequently make the same mistake. Once you know it, it is however easy to fix.

The fix is to not write &; but rather just write one of the two separators depending on your intention. (And in most cases where one has written &; the intention was to only write &).

This should work:

ssh server 'sudo shutdown -r +5 & disown'