Big Shield
Big Shield

Reputation: 622

Why the process started by a bash script will not exit after the terminal has been closed?

Case 1:

I execute sleep 1000 & in gnome-terminal and then close the terminal. The sleep process does not exist.

Case 2:

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

Answers (3)

pynexj
pynexj

Reputation: 20688

What Jürgen Hötzel says in the answer is true but that's not exactly what's happening.

Case 1:

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 the SIGHUP to all jobs, running or stopped. Stopped jobs are sent SIGCONT to ensure that they receive the SIGHUP.

  • If the huponexit shell option has been set with shopt, bash sends a SIGHUP 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.)

Case 2:

Here there are 2 bash processes involved:

  • bash#1: The bash process which is running in the gnome-terminal.
  • bash#2: The bash process which runs the 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

Jürgen Hötzel
Jürgen Hötzel

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

Shamindra Parui
Shamindra Parui

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

Related Questions