Nohup over ssh won't return

11

3

I am trying to do

[me@myPc]$ ssh me@server "nohup myBashScript.sh &"

My goal is to launch the process on the server, and then immediately return.

It is not working: The job is started on server, but I still get the output on myPc and bash wait for completion prior to asking me for another command.

Why ? It's not supposed to ! Any way to avoid that ?

  • myPc is RHEL6.2
  • server is ubuntu 10.04 and
  • both runs openssh

user1219721

Posted 2012-07-15T17:24:30.333

Reputation: 273

Answers

11

As long as input or output are still open, ssh will keep the connection open. To solve this, make sure the input and output are not open.

For instance, use ssh -n and redirect your output:

ssh -n me@example.com "nohup myscript.sh >/dev/null 2>&1 &"

Michael Hampton

Posted 2012-07-15T17:24:30.333

Reputation: 11 744

What if I still want myscript.sh to give me output, but the ssh session to still terminate after the script is done? In my case, the script itself starts nohup process, then returns. I was under the impression that ssh -T would help me do that. – Gauthier – 2019-06-06T22:57:48.413

@Gauthier Then just run the script without nohup. – Michael Hampton – 2019-06-07T03:04:30.780

I do. The script starts other commands with nohup and &, then terminates. But ssh doesn't return. The commands called in the script redirect stdout to files. Oh, but not stderr and stdin, could it be why? – Gauthier – 2019-06-07T10:36:36.450

There you have it! Input and output must be redirected. – Michael Hampton – 2019-06-07T18:14:50.380

What I don't understand is that if I actually login and get a tty, start the script that starts the nohup& commands, then I am allowed to logout from the ssh session. It doesn't complain that there are open input and output. Isn't that what ssh -t is supposed to mimic? – Gauthier – 2019-06-07T18:29:16.160