This is a followup to this question.
I've run some more tests; looks like it really doesn't matter if this is done at the physical console or via SSH, neither does this happen only with SCP; I also tested it with cat /dev/zero > /dev/null
. The behaviour is exactly the same:
- Start a process in the background using
&
(or put it in background after it's started usingCTRL-Z
andbg
); this is done without usingnohup
. - Log off.
- Log on again.
- The process is still there, running happily, and is now a direct child of
init
.
I can confirm both SCP and CAT quits immediately if sent a SIGHUP
; I tested this using kill -HUP
.
So, it really looks like SIGHUP is not sent upon logoff, at least to background processes (can't test with a foreground one for obvious reasons).
This happened to me initially with the service console of VMware ESX 3.5 (which is based on RedHat), but I was able to replicate it exactly on CentOS 5.4.
The question is, again: shouldn't a SIGHUP be sent to processes, even if they're running in background, upon logging off? Why is this not happening?
Edit
I checked with strace
, as per Kyle's answer.
As I was expecting, the process doesn't get any signal when logging off from the shell where it was launched. This happens both when using the server's console and via SSH.
Answer found.
For BASH, this depends on the
huponexit
shell option, which can be viewed and/or set using the built-inshopt
command.Looks like this options is off by default, at least on RedHat-based systems.
More info on the BASH man page:
It will be sent SIGHUP in my tests:
Shell1:
Shell2:
Shell1 Again:
Shell2 Again:
Why does it still run?:
Advanced Programing in the Unix Environment by Stevens covers this under section 9.10: Orphaned Process Groups. The most relevant section being:
I ran some tests using CentOS 7.1 and bash. Note this means
huponexit
isoff
by default, and was off for the majority of my tests.You need
nohup
when you start a job in a terminal, because if you close that terminal without exiting the shell cleanly, the terminal sends bash the SIGHUP signal to the shell, which then sends it to all children. If you exit the shell cleanly- meaning the job must already be in the background so you can typeexit
or hit Control-D at the command prompt- no signals of any sort are sent to the background job from bash.Test:
Terminal 1
Terminal 2
(close terminal 1, seen in terminal 2):
Job
doit.sh
:Start it in the background in Terminal 1:
Terminal 1
Strace it in Terminal 2; close Terminal 1 after a couple of loops:
Terminal 2
Output in Terminal 3:
Terminal 3
However, if you exit
bash
, it simply exits without sending any signal to the child at all. The terminal will exit because it no longer has a child, but of course there is no one to HUP because the child shell is already gone. TheSIGINT
,SIG_BLOCK
andSIG_SETMASK
you see below are due to thesleep
in the shell.Terminal 1
Terminal 2
Terminal 3, output
Interestingly, I set
huponexit
to be on withshopt -s huponexit; shopt
(the latter shopt to review), then performed the last test, and again bash sent no signal to the background process. Even MORE interstingly, as we have seen bash did send the signal to the background process after it received it from a terminal that closed in its face. It seems as thoughhuponexit
had no bearing one way or the other.I hope this removes any mystery or confusion regarding at least bash's huppiness, about when and how the HUP signal is sent. At least my tests were completely reproducible, for me. I would be interested to know if there are any other settings that may be affecting bash's behavior.
And, as always, YSMV (Your Shell May Vary).
Addendum 1
When I run a shell as
exec /bin/sh
, then run the script as/bin/sh ./doit.sh &
, then exit the shell cleanly, no signals are sent to the background job and it continues to run to completion.Addendum 2
When I run a shell as
exec /bin/csh
, then run the script as/bin/sh ./doit.sh &
, then exit the shell cleanly, no signals are sent to the background job and it continues to run to completion.I use csh and background processes continue running along when I logoff.