Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How bash handles the jobs when logout?

Tags:

linux

bash

nohup

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 ?

like image 690
Unni Avatar asked Nov 28 '10 20:11

Unni


People also ask

How do you quit a job in bash?

The kill command can kill jobs or send signals to them. The disown command removes a job from the list of jobs (without killing it). A foreground job can be suspended by typing ^Z (Control-Z). A suspended job is temporarily stopped.

What is bash job control?

Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point. A user typically employs this facility via an interactive interface supplied jointly by the system's terminal driver and Bash. The shell associates a job with each pipeline.

How do you stop background employment?

z/OS UNIX System Services User's Guide To cancel a background job, use the kill command. To be able to kill a process, you must own it. (The superuser, however, can kill any process except init.) Before you can cancel a background job, you need to know either a PID, job identifier, or PGID.

Which command will list all stopped and background processes that the shell is controlling?

To list all stopped or backgrounded processes, you can use the jobs command: jobs.


2 Answers

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
    
like image 70
jilles Avatar answered Oct 12 '22 09:10

jilles


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.

like image 24
thejh Avatar answered Oct 12 '22 09:10

thejh