Discussion:
Unable to disown process
Andrew DeFaria
2011-07-06 17:24:08 UTC
Permalink
I have the following script that I use to tunnel nntp traffic:

#!/bin/bash
nohup ssh -NL 1119:news.mozilla.org:119 \
-L 2119:news.gmane.org:119 \
-L 3119:nntp.perl.org:119 \
***@defaria.com > /dev/null 2>&1 &

disown

If I run this script it works fine and I'm tunneled. The problem is I
cannot exit the terminal in which I invoked this script. When I attempt
to do this the terminal hangs. I can close the terminal (mintty BTW)
forcefully and the ssh session remains but it was my understanding that
disown should allow you to exit the current shell with the
backgrounded/disowned process continuing to run. In fact I tested this
on RHEL and it worked as expected. What's up with Cygwin?
--
Andrew DeFaria <http://defaria.com>
Part of the inhumanity of the computer is that, once it is competently
programmed and working smoothly, it is completely honest. - Isaac Asimov
Andy Koppe
2011-07-06 19:12:12 UTC
Permalink
  #!/bin/bash
  nohup ssh -NL 1119:news.mozilla.org:119 \
        -L 2119:news.gmane.org:119 \
        -L 3119:nntp.perl.org:119 \
  disown
If I run this script it works fine and I'm tunneled. The problem is I cannot
exit the terminal in which I invoked this script. When I attempt to do this
the terminal hangs. I can close the terminal (mintty BTW) forcefully and the
ssh session remains but it was my understanding that disown should allow you
to exit the current shell with the backgrounded/disowned process continuing
to run. In fact I tested this on RHEL and it worked as expected. What's up
with Cygwin?
Mintty stays open as long as any processes are connected to it, i.e.
until it gets an EOF from its underlying pseudo terminal device,
whereas other terminals quit as soon as their direct child process
finishes. The Cygwin console behaves the same as mintty in this regard
though.

Nohup doesn't disassociate a process from its terminal, it just blocks
the SIGHUP signal for indicating when the terminal is gone. And
'disown' removes a job from the shell's job table, but again, the
job's processes will remain connected to the terminal.

You can use the 'setsid' utility from the util-linux package to invoke
a program in its own session, i.e. without connection to the terminal
it is invoked from. For example:

setsid ssh -NL 1119:news.mozilla.org:119 \
-L 2119:news.gmane.org:119 \
-L 3119:nntp.perl.org:119 \
***@defaria.com

No need for 'nohup', redirections, backgrounding, or 'disown' with this.

Andy
Andrew DeFaria
2011-07-06 20:15:11 UTC
Permalink
Post by Andy Koppe
Post by Andrew DeFaria
#!/bin/bash
nohup ssh -NL 1119:news.mozilla.org:119 \
-L 2119:news.gmane.org:119 \
-L 3119:nntp.perl.org:119 \
disown
If I run this script it works fine and I'm tunneled. The problem is I cannot
exit the terminal in which I invoked this script. When I attempt to do this
the terminal hangs. I can close the terminal (mintty BTW) forcefully and the
ssh session remains but it was my understanding that disown should allow you
to exit the current shell with the backgrounded/disowned process continuing
to run. In fact I tested this on RHEL and it worked as expected. What's up
with Cygwin?
Mintty stays open as long as any processes are connected to it, i.e.
until it gets an EOF from its underlying pseudo terminal device,
whereas other terminals quit as soon as their direct child process
finishes. The Cygwin console behaves the same as mintty in this regard
though.
Ah ha!
Post by Andy Koppe
Nohup doesn't disassociate a process from its terminal, it just blocks
the SIGHUP signal for indicating when the terminal is gone. And
'disown' removes a job from the shell's job table, but again, the
job's processes will remain connected to the terminal.
You can use the 'setsid' utility from the util-linux package to invoke
a program in its own session, i.e. without connection to the terminal
setsid ssh -NL 1119:news.mozilla.org:119 \
-L 2119:news.gmane.org:119 \
-L 3119:nntp.perl.org:119 \
Excellent description and solution. Thanks.
Post by Andy Koppe
No need for 'nohup', redirections, backgrounding, or 'disown' with this.
Of course those were all the desperate things I was trying to get this
to work...

Actually I still need backgrounding as without it my ~/bin/tunnel just
hangs on the setsid command...
--
Andrew DeFaria <http://defaria.com>
What is the speed of dark?
Andy Koppe
2011-07-06 21:01:00 UTC
Permalink
Post by Andy Koppe
You can use the 'setsid' utility from the util-linux package to invoke
a program in its own session, i.e. without connection to the terminal
setsid ssh -NL 1119:news.mozilla.org:119 \
         -L 2119:news.gmane.org:119 \
         -L 3119:nntp.perl.org:119 \
No need for 'nohup', redirections, backgrounding, or 'disown' with this.
Actually I still need backgrounding as without it my ~/bin/tunnel just hangs
on the setsid command...
Hmm, I'm afraid I don't know why that happens when setsid is invoked
from a script.

This returns immediately:

$ setsid sleep 10

As does this:

$ bash -c 'setsid sleep 10'

But this doesn't:

$ echo 'setsid sleep 10' > sleep.sh; bash sleep.sh

Andy
Andrew DeFaria
2011-07-06 21:12:52 UTC
Permalink
Post by Andy Koppe
Post by Andy Koppe
You can use the 'setsid' utility from the util-linux package to invoke
a program in its own session, i.e. without connection to the terminal
setsid ssh -NL 1119:news.mozilla.org:119 \
-L 2119:news.gmane.org:119 \
-L 3119:nntp.perl.org:119 \
No need for 'nohup', redirections, backgrounding, or 'disown' with this.
Actually I still need backgrounding as without it my ~/bin/tunnel just hangs
on the setsid command...
Hmm, I'm afraid I don't know why that happens when setsid is invoked
from a script.
$ setsid sleep 10
$ bash -c 'setsid sleep 10'
$ echo 'setsid sleep 10'> sleep.sh; bash sleep.sh
Andy
The above coincides with my experiments too. Not a big deal as adding a
"&" to the line in the script works just fine.
--
Andrew DeFaria <http://defaria.com>
If croutons are stale bread, why do they come in airtight packages?
Continue reading on narkive:
Loading...