1

When I run on linux-system/bash:

sleep 10 &

I get bash prompt immediately.

But when I run:

ssh linux-system 'sleep 10 &'

I wait 10 seconds.

Two questions:

  1. Can I find explanation in ssh/bash documentation ?

  2. By changing context/configuration, can we make ssh to return immediately, without changing the command sleep 10 & itself? for example :

ssh linux-system 'change-bash-configuration; sleep 10 &'

or

ssh -some-ssh-options linux-system 'sleep 10 &'

Update 1

We can use ssh -f to get the prompt, but a background ssh process still waits for remote process to finish. So my questions remain.

Update 2

To clarify, I want local ssh process to terminate (not waiting for 10 seconds in the background) after starting remote job in the background.

1

2 Answers 2

1

According to the ssh man page

The session terminates when the command or shell on the remote machine exits and all X11 and TCP connections have been closed.

The reason ssh doesn't exit right away is because the remote command still has stdin, stdout, stderr open. If you close them, it should return immediately.

ssh localhost "sleep 10 < /dev/null &> /dev/null &"

This replaces stdin, stdout, and stderr for sleep. Bash still has those open. But since sleep is now in the background, since this is not an interactive bash shell, it leaves bash with nothing left to do and it exits, closing the connections that the remote sshd provided for stdin, stdout, and stderr. Since at that point there are no open connections for ssh, it can close the ssh session and exit.

To extend this example a bit, if you ran

ssh -X localhost "(xclock & sleep 1) < /dev/null &> /dev/null"

then ssh would establish an X11 tunnel which xclock would then open a connection through. (The sleep is needed to give xclock time to start before bash exits.) Bash would then exit, but the X11 connection would persist and ssh would not exit. If you killed ssh, the xclock would lose its connection through the tunnel and the window would close. If instead you close or kill the xclock window itself, xclock will close the X11 connection, and since that's the last connection, ssh would then exit.

3
  • Comments have been moved to chat; please do not continue the discussion here. Before posting a comment below this one, please review the purposes of comments. Comments that do not request clarification or suggest improvements usually belong as an answer, on Unix & Linux Meta, or in Unix & Linux Chat. Comments continuing discussion may be removed.
    – terdon
    Commented Jul 14 at 12:27
  • You probably don’t need the < /dev/null, since background (asynchronous) processes get their stdin redirected to /dev/null by default. Commented Jul 14 at 21:42
  • It is needed unless you use -f or -n at least with older versions of ssh and commands that might try to take input.
    – user10489
    Commented Jul 15 at 0:08
1

The behavior is the same, actually. What happens is that you are sending the sleep to the background on the remote machine, but you are not sending the ssh command to the background on your local machine. So the local machine waits for the ssh to finish, and the ssh waits for the sleep to finish.

If you want to get your local prompt back immediately, you can send the ssh to the background:

ssh linux-system 'sleep 10' &

However, this will likely hit another issue, so if this is the behavior you want, use -f:

ssh -f linux-system 'sleep 10'

Or make sure no input goes to the ssh command from the terminal with

ssh linux-system 'sleep 10' </dev/null

If you want the command to continue on the remote server, even if you close your local terminal, you can use nohup:

ssh linux-system 'nohup sleep 10 &' </dev/null &

or

ssh -f linux-system 'nohup sleep 10 &' </dev/null &

As for where this is documented, it is unlikely to be explicitly mentioned, this isn't a special feature or anything, it is simply that your original version wasn't sending the ssh to the background.

21
  • I don't want to start ssh in background, but make it to terminate.
    – Philippe
    Commented Jul 13 at 11:02
  • @Philippe I don't understand what that means. Can you clarify?
    – terdon
    Commented Jul 13 at 11:03
  • @Philippe You said before that you just wanted to understand, not that you wanted to have any particular effect. Please update your question with what you want.
    – Kusalananda
    Commented Jul 13 at 11:03
  • For example, why there is no difference between ssh linux-system 'sleep 10' and ssh linux-system 'sleep 10 &' ?
    – Philippe
    Commented Jul 13 at 11:10
  • 1
    @Philippe then please say that in your question. Sounds like you should be using screen or tmux. Ideally, also explain why you want this because that will help us understand hat you really need. Why do you not want to have the ssh running? The resources it consumes are negligible, so is it that you want to be able to turn off your local machine and leave the command running remotely? If so, that's what screen and tmux are for.
    – terdon
    Commented Jul 13 at 11:37

You must log in to answer this question.

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