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.
You have already found the right way, here document.
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 &"
Use Here document
The above 3 options should work for you.
In addition, take a look at the answer here: https://askubuntu.com/a/348921/70270
is not running in daemon mode, keeping your ssh session connected. Ssh session will return in 10 seconds, despite you used nohup.
Reason is that remote stdout and stderr still connected to your session. It keeps ssh session alive, nohup does not help.
This:
returns immediately. It does start remote process with nohup and exits ssh session immediately.
Why not just tmux or screen and be done with it? For example:
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.
Shorter form:
It appears, that bash itself performs detaching from terminal, so no nohup needed. I run Ubuntu 14.04 x86_64, bash 4.3.11.
And last but not least, remember not to allocate a tty for your session.
If you do that, it will not work.
So if you got any
-t
or-tt
or-ttt
and so on in your command. Thats the reason why it is not working for you.So instead of
Use