11

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

2 Answers 2

13

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 [email protected] "nohup myscript.sh >/dev/null 2>&1 &"
5
  • 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
    Commented Jun 6, 2019 at 22:57
  • @Gauthier Then just run the script without nohup. Commented Jun 7, 2019 at 3:04
  • 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
    Commented Jun 7, 2019 at 10:36
  • There you have it! Input and output must be redirected. Commented Jun 7, 2019 at 18:14
  • 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
    Commented Jun 7, 2019 at 18:29
0

Is it really necessary to run nohup via ssh? In my tests I did not find ANY difference running command with and without nohup. It seems that ssh does not send HUP anyway. This works equally as with nohup:

ssh me@server "myBashScript.sh >/dev/null 2>&1 &"

The key to solution here is redirecting stdout and stderr as already mentioned in the accepted answer. Without it SSH reads from those descriptors, and returns only after they are closed.

4
  • Seriously doubt?) I literally put the command that works. I put it as a separate answer because it differs a lot from "another answer", and I doubt that the answer will be changed. As for no explanation about redirection - agree, added.
    – Alek
    Commented Mar 16, 2023 at 19:54
  • not only nohup, but also no stdin redirection. Ok, thanks, I think twice next time.
    – Alek
    Commented Mar 16, 2023 at 21:21
  • Actually the observation about stdin does justify a separate answer. I missed it because you originally concentrated on nohup. In a case like this it may be good to clearly state why your answer is better than the old answer. Example: "I know this old answer (link) solves the problem, but I think it is overzealous with -n. My tests indicate that stdin doesn't matter, -n is not needed; it's enough to redirect stdout and stderr, like this: … Also nohup is not needed. In my tests …". The solution first, simplification due to lack of nohup as a bonus. Commented Mar 16, 2023 at 21:37
  • No problem, thanks for the suggestion, I'll take to account next time.
    – Alek
    Commented Mar 16, 2023 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .