[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash -c 'sleep 5 &' vs. ssh user@host 'sleep 5 &'

From: Robert Elz
Subject: Re: bash -c 'sleep 5 &' vs. ssh user@host 'sleep 5 &'
Date: Tue, 22 Sep 2020 13:01:33 +0700

    Date:        Tue, 22 Sep 2020 10:02:07 +0800
    From:        Clark Wang <dearvoid@gmail.com>

  | In an interactive shell (in case this matters), run bash -c 'sleep 5 &' and
  | it exits immediately without waiting for the sleep to complete. But ssh
  | user@host 'sleep 5 &' (with bash as the login shell) would wait for sleep
  | to complete. Why the difference?

It isn't bash waiting for the sleep to complete, it is ssh waiting for
the connection to close.  Neither ssh nor the sshd at the target know that
"sleep" doesn't write any output, so they have to keep the connection alive
(and hencs ssh running on the client end) until the command has completed
(actually, until it closes its fds, so you could do

        ssh user@host 'sleep 5 >&- 2>&- &'

if you wanted (no need to worry about stdin, because it is an async
process, that gets connected to /dev/null anyway).   This shows
stdout & stderr being closed - that part is not important (just the
briefest sane example to give) - you could also redirect them to
anywhere else (just remember that it has to be both of them).


reply via email to

[Prev in Thread] Current Thread [Next in Thread]