I have a bash script (on the local machine) that has the following line

ssh root@remoteip "some commands; shutdown -r now"

#do other things

However, after this line (the remote server was rebooted successfully), the whole bash script just exits ("do other things" is not executed).

How to fix this problem?

  • What do you mean by do other things? is that a part of your script? It's a little bit unclear what you are asking for! – Ravexina Apr 25 '17 at 9:52
  • Ya, "do other things" are a part of the bash script – aye Apr 25 '17 at 9:53
  • You don't have to fix it, it's not a bug or unexpected behaviour. What you can do, though, is create 2 different cripts: i) "some commands; shutdown -r now" and ii) do other things. Where the ii is ran after reboot. – M. Becerra Apr 25 '17 at 9:53
  • Is there anyway to tell the ssh remote command to ignore the output (just issues those commands, no need to care for the output, and continue with do other things) – aye Apr 25 '17 at 10:01
  • @MBecerra, OP's script runs on the local machine, not on the one that gets rebooted. – Andrea Lazzarotto Apr 25 '17 at 10:07

Run the shutdown command in background, this did the job for me:


ssh root@remote-ip "some-command > /dev/null; shutdown -r now &"

uname -a
  • some-command > /dev/null: runs some-command and redirects the output to /dev/null, so I don't get any output as you said.
  • shutdown -r now &: runs the shutdown command in background and leaves the shell for getting further commands.
  • uname is my #do other things for test purpose, which will be executed right after remote system is going down; )does not waits for it to be complete).

There are other ways to achieve this result too, e.g you can run the whole command in background:

ssh root@remote-ip "some-command > /dev/null; shutdown -r now" &

Or run it in a sub shell:

$(ssh root@remote-ip "some-command; shutdown -r now")

Although I suggest the first one, meaning running the shutdown command in the background of remote machine.

  • The first way is perfect. The other two ways seem a bit hacky because probably they may leave a running SSH process until some timeout occurs. – Andrea Lazzarotto Apr 25 '17 at 11:35
  • Yeah, I did an update ;) – Ravexina Apr 25 '17 at 11:56

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.