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?

  • 1
    I should add that I'm trying to send the shutdown command to background with the "&". If the shell is interpreting the "&" as a command separator then I'm stumped as to how to send it to background so that the "disown" command works.
    – Horace
    Feb 5 '15 at 17:34
  • The & character does send the command on the left hand side in the background. That's how it differs from ;.
    – kasperd
    Feb 5 '15 at 17:39

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'
  • 7
    You win today's prize for teaching me something I didn't know after 25 years of bashing on UNIX. Also +1!
    – MadHatter
    Feb 5 '15 at 8:20
  • What if he intended to run sudo shutdown -r +5 & on background and then run disown? How would bash handle such situation? I would expect this to be perfect syntax for a command, even having &;. If ; is a command separator, then you have 2 commands on your hands. Right? Feb 5 '15 at 11:10
  • 1
    @IsmaelMiguel I don't get your question. Using & will start the first command in the background and once the first command has been started the second command will be run in the foreground.
    – kasperd
    Feb 5 '15 at 12:22
  • I meant that sudo shutdown -r +5 &; disown doesn't seem like a bad syntax. I mean, my intention could be to run the first part in background and then the 2nd part. Feb 5 '15 at 12:25
  • @IsmaelMiguel The command parser could probably be extended to consider the empty string to be a valid command which always succeeds. In that case sudo shutdown -r +5 &; disown would be considered to be three valid commands separated by & and ;. It would however mean that one has to be careful not to use an additional separator if the exist status of the previous command is to be used. It does mean that one has to pay attention to tokenizing as well since for example && and & & gets tokenized differently.
    – kasperd
    Feb 5 '15 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.