21

As far as I understood from the books and bash manuals is that. When a user logs out from bash all the background jobs that is started by the user will automatically terminate, if he is not using nohup or disown. But today I tested it :

  1. Logged in to my gnome desktop and accessed gnome-terminal.
  2. There are two tabs in the terminal and in one I created a new user called test and logged in as test

    su - test
    
  3. started a script.

    cat test.sh
    #!/bin/bash
    sleep 60
    printf "hello world!!"
    exit 0
    
    
    ./test.sh &
    
  4. After that I logged out of test and closed the tab

  5. In the next tab I exected ps aux as root and found that job is still running.

How this is happening ?

Kos
  • 70,399
  • 25
  • 169
  • 233
Unni
  • 1,721
  • 2
  • 14
  • 12
  • I noticed that this question does not yet have an accepted answer and that there is a similar new question on [SU] which might be interesting reading: [http://superuser.com/questions/662431/what-exactly-determines-if-a-backgrounded-job-is-killed-when-the-shell-is-exited](http://superuser.com/questions/662431/what-exactly-determines-if-a-backgrounded-job-is-killed-when-the-shell-is-exited) – Hennes Oct 21 '13 at 16:13

2 Answers2

19

Whether running background jobs are terminated on exit depends on the shell. Bash normally does not do this, but can be configured to for login shells (shopt -s huponexit). In any case, access to the tty is impossible after the controlling process (such as a login shell) has terminated.

Situations that do always cause SIGHUP include:

  • Anything in foreground when the tty is closed down.
  • Any background job that includes stopped processes when their shell terminates (SIGCONT and SIGHUP). Shells typically warn you before letting this happen.

huponexit summary:

  • On: Background jobs will be terminated with SIGHUP when shell exits

    $ shopt -s huponexit
    $ shopt huponexit
    huponexit       on
    
  • Off: Background jobs will NOT be terminated with SIGHUP when shell exits.

    $ shopt -u huponexit
    $ shopt huponexit
    huponexit       off
    
wjandrea
  • 28,235
  • 9
  • 60
  • 81
jilles
  • 10,509
  • 2
  • 26
  • 39
  • 2
    so you are saying that, if huponexit option is not set then bash will not kill the background jobs of users ? – Unni Dec 02 '10 at 00:05
  • @Unni – If `huponexit` is set, Bash will send SIGHUP to all jobs when an interactive login shell exits ( with either **exit** or **Ctrl + d** ). – Travis Clarke Mar 02 '17 at 04:44
3

Only interactive shells kill jobs when you close them. Other shells (for example those you get by using su - username) don't do that. And interactive shells only kill direct subprocesses.

thejh
  • 44,854
  • 16
  • 96
  • 107
  • What is interactive shell ? http://tldp.org/LDP/abs/html/intandnonint.html says that, if $PS1 is set then it is an interactive shell. Also I switched to another tty using ctrl+alt+f2 and logged in as test and done same thing. result is same but. – Unni Nov 28 '10 at 20:37
  • 1
    `su - username` gives you an interactive shell. – Dennis Williamson Nov 28 '10 at 20:59