I want to execute a script, start.sh on a remote server which runs this:

nohup node server.js &

Naively, I call SSH like this:

ssh myserver <<EOF
./start.sh &
EOF

This starts the script, but leaves the session connected. I want to follow this step with other commands in a script, so that's no good.

How can I SSH to the remote machine, launch a nohup command into the background, then disconnect? I suppose I could put the SSH process itself into the background, but that doesn't seem right.

up vote 35 down vote accepted

You have already found the right way, here document.

NOTE: you can put the ssh (client) into background by placing a & at the end, but you will not see the output. If you really want to do this, redirect the stdout/stderr to a file in case you need to check the response from the remote host.

Basically you can do it in either way:

Directly run the command{,s}

ssh user@host "nohup command1 > /dev/null 2>&1 &; nohup command2; command3"

OR

ssh user@host "$(nohup command1 > /dev/null 2>&1 &) && nohup command2 >> /path/to/log 2>&1 &"

NOTE: && requires the first command to return 0 before executing the second

Use Here document

ssh user@host << EOF
nohup command1 > /dev/null 2>&1 &
nohup command2 >> /path/to/command2.log 2>&1 &
......
EOF

The above 3 options should work for you.

In addition, take a look at the answer here: https://askubuntu.com/a/348921/70270

  • Thanks - so the bit I was missing was really the > /dev/null. – Steve Bennett Sep 24 '13 at 0:04
  • Not really, > /dev/null 2>&1 is to discard any stdout/stderr output by redirecting to the black hole lol – Terry Wang Sep 24 '13 at 0:18
  • 5
    Note that I would suggest using >> instead of just > to write to log files. Otherwise, you'll only get the errors from the very last run. – Alexis Wilke Sep 24 '13 at 0:24
  • Is it correct that if I simply start a command as nohup command2 >> /path/to/command2.log 2>&1 & in a terminal and then close the terminal that the process is lost? – Luke Apr 7 '14 at 11:00
  • 1
    @TerryWang the second example will not work with ampersand followed by double ampersand, see unix.stackexchange.com/q/67006/86716 – tsionyx Dec 9 '16 at 14:18

Why not just tmux or screen and be done with it? For example:

$ tmux new -s SessionNameHere
$ nohup /path/to/your/script.sh

This is practical if it's something that will continuously loop or take a while to finish. You can disconnect from the session and it will remain active.

  • 1
    Doesn't really fit what I'm asking for, which is a single command that can be run on my laptop, which will connect, fork a command, then disconnect. – Steve Bennett Nov 13 '14 at 22:24

Shorter form:

ssh host "(command 1; command 2; ...) &>/dev/null &"

It appears, that bash itself performs detaching from terminal, so no nohup needed. I run Ubuntu 14.04 x86_64, bash 4.3.11.

  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. - From Review – David Foerster Feb 28 '16 at 9:34
  • This doesn't seem to work. nohup; just outputs a complaint about missing arguments. – Steve Bennett Feb 29 '16 at 4:32
  • @DavidFoerster, no Idea why, if command 1; command 2; ... represents script OP want to run. OP can also use special case of this answer: ssh myserver ./start.sh &>/dev/null & – Alek_A Feb 29 '16 at 10:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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