SSH connection hanging on logout

Jason Stone jason at shalott.net
Sat May 5 21:54:00 EST 2001


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> About the hang-on-exit bug: this is the TODO item which shows up when you
> run "ssh server 'sleep 20 & exit'".
> 
> * The shell starts up, and starts its own session.  As a side-effect, it
>   gets its own process group.
> * The sshd server attempts to read from the master side of the pty, and
>   while there are still process with the pty open, no EOF is produced.
> * The sleep command exits, closes its descriptor, sshd detects the EOF, and
>   the connection gets closed.

Or, put another way, this is a feature, not a bug - sshd has no way of
knowing that "sleep 20" isn't going to eventually produce some output that
you'll want to see, so it stays alive until the background command exits.

The real "bug" is users trying to use the shell's '&' builtin to run
daemon processes.  If you want a command to really be backgrounded (ie, to
daemonize), use something other than '&', something that will make the
command close the pty and either start its own process group or else
become a child of init.  Eg:

perl -e 'fork && exit; close STDIN; close STDOUT; close STDERR; \
setpgrp(0,$$); exec "sleep 20";'

(Watch out for the quoting if you try this on the commandline....)


> Known-good workarounds:
> * bash: shopt huponexit on
> * tcsh: none

* zsh: setopt HUP
(this is usually the default)


If you use zsh, you might also try something like this in your .zshrc:

daemonize(){
        COMMAND="$@"
        perl -e 'fork && exit; close STDIN; close STDOUT; close STDERR; \
                setpgrp(0,$$); exec "'$COMMAND'";' }
}

You would then run "daemonize sleep 20" and the sleep 20 would be run in
the background and not hang the sshd when you exit.

This will almost certainly work in other bourne-compatible shells as well.


- -Jason

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (FreeBSD)
Comment: See https://private.idealab.com/public/jason/jason.gpg

iD8DBQE68+nhswXMWWtptckRAjaVAJ0bbN7PPe0jLC80SPZjDNAvBFuC2wCaA4ep
1IteXaTPMxe2TsKrsLmg20A=
=mEVt
-----END PGP SIGNATURE-----




More information about the openssh-unix-dev mailing list