2

Would someone explain if & why disown is necessary if you wish to keep a job running even after you disconnect.

I ask because every site I have visited says to use disown and bg but bg on its own is working for me and I'm not sure why.

Is it because I haven't fully understood what disown is for or are there some settings somewhere that are affecting the default behavior of the bg command?

Here's what I did:

  • Connected to my CentOS 6 box via SSH
  • Created a simple process:

    tar zcvf example.tar.gz ./examplepath > /dev/null 2>&1
    
  • Suspended the job

  • Resumed it in background via bg 1
  • Disconnected from SSH server

I then checked via FTP to see if the .tar.gz file was still growing in size and it was.

4

Processes backgrounded via bg or & will typically die under 2 scenarios:

  1. The shell receives a SIGHUP
  2. They try to write to a terminal which no longer exists.

Item #1 is the primary culprit when closing your terminal. However whether it happens or not depends on how you close your terminal. You can close it by:

  1. Something like clicking the "X" in your window manager
  2. You can type exit, logout, or CTRL+D.

Item #1 is the one that will result in a SIGHUP being sent. #2 does not.

So long story short, if you background a process with bg, and then log out with exit, logout, or CTRL+D, the process will not be killed.

  • Thanks. The logout or CTRL+D method not resulting in SIGHUP explains everything. – E-71 Jun 20 '14 at 18:47
1

When you close a terminal, all processes that uses this terminal (see TTY column in ps -elf output) receive a SIGHUP. It's up to process what to do on SIGHUP. In classic unixes (AIX, Solaris), process are closed on this signal. That's why you need to use disown. From man bash:

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. To prevent the shell from sending the signal to a particular job, it should be removed from the jobs table with the disown builtin (see SHELL BUILTIN COMMANDS below) or marked to not receive SIGHUP using disown -h.

  • Are you saying the behavior varies by process and in my case, tar doesn't close in response to a SIGHUP but another process might? – E-71 Jun 20 '14 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.