Reputation: 622
I execute sleep 1000 &
in gnome-terminal and then close the terminal. The sleep
process does not exist.
Here's a script t.sh
:
#!/usr/bin/env bash
sleep 1000 &
I execute ./t.sh
in gnome-terminal and then close the terminal. The sleep
process exists (known by ps aux
). Should not the process be closed?
Upvotes: 2
Views: 2034
Reputation: 20688
What Jürgen Hötzel says in the answer is true but that's not exactly what's happening.
When the gnome-terminal
is closed, the tty (including pty) driver would get a disconnect event of the tty and send SIGHUP
to the controlling process associated with the tty. Here the controlling process is bash and bash will receive the SIGHUP
. Then, according to the bash manual:
The shell exits by default upon receipt of a
SIGHUP
. Before exiting, an interactive shell resends theSIGHUP
to all jobs, running or stopped. Stopped jobs are sentSIGCONT
to ensure that they receive theSIGHUP
.If the
huponexit
shell option has been set withshopt
, bash sends aSIGHUP
to all jobs when an interactive login shell exits.
So for Case 1, bash will resend SIGHUP
to the background job sleep
which will be killed by the signal (this is the default behavior of SIGHUP
).
(The huponexit
option mentioned by Jürgen Hötzel only affects interactive login shells which exit voluntarily. But for Case 1, the bash is killed by SIGHUP
. And, the bash running in gnome-terminal is not necessarily a login shell though it's really an interactive shell.)
Here there are 2 bash processes involved:
t.sh
script.When t.sh
(bash#2) is running, sleep 1000 &
starts as a background job of bash#2. After t.sh
(bash#2) exits, the sleep
will continue running but it'll become an orphan process and the init
process will take care of it and sleep
's PPID will become 1 (PID of the init
process).
When the gnome-terminal is closed, bash#1 will receive SIGHUP
and bash#1 will resend SIGHUP
to all it's jobs. But the sleep
is not a job of bash#1 so sleep
will not receive the SIGHUP
so it'll continue running after bash#1 completes.
Upvotes: 3
Reputation: 19717
When using an interactive login shell, bash will send a HUP signal to your jobs on exit if this option is set:
juergen@samson:~ → shopt huponexit
huponexit on
The default handler will terminate your process. When starting a non-interactive bash (like your script) the huponexit option is effectless. See: https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
Upvotes: 4
Reputation: 56
In simple words, no. With "&" your are making the current process to run as background process. And you must kill it explicitly, according to my knowledge.
Upvotes: -1