6

From gnome-terminal I know the ability to suspend a job with C-z, and then send it to the background. When I close the terminal the process does not end. Where is the job being managed from, or is it lost?

  • 1
    This looks almost as a duplicate of a question I've asked, though expressed differently. Have a look for some insightful answers. – rozcietrzewiacz Nov 18 '11 at 22:05
  • 2
    Why do you think it does not end? When you close the terminal, a SIGHUP signal is sent to all processes attached to it, which normally kills them. – psusi Nov 19 '11 at 00:49

3 Answers3

11

Your background job continues executing until someone tells it to stop by sending it a signal. There are several ways it might die:

  • When the terminal goes away for any reason, it sends a HUP signal (“hangup”, as in modem hangup) to the shell running inside it (more precisely, to the controlling process) and to the process in the foreground process group. A program running in the background is thus not affected, but…
  • When the shell receives that HUP signal, it propagates it to the background jobs. So if the background process is not ignoring the signal, it dies at this point.
  • If the program tries to read or write from the terminal after it's gone away, the read or write will fail with an input/output error (EIO). The program may then decide to exit.
  • You (or your system administrator), of course, may decide to kill the program at any time.

If your concern is to keep the program running, then:

  • If the program may interact with the terminal, use Screen or Tmux to run the program in a virtual terminal that you can disconnect from and reconnect to at will.
  • If the program just needs to keep running and is not interactive, start it with the nohup command (nohup myprogram --option somearg), which ensures that the shell won't send it a SIGHUP, redirects standard input to /dev/null and redirects standard output and standard error to a file called nohup.out.
  • If you've already started the program and don't want it to die when you close your terminal, run the disown built-in, if your shell has one. If it doesn't, you can avoid the shell's propagation of SIGHUP by killing the shell with extreme prejudice (kill -KILL $$ from that shell, which bypasses any exit trigger that the indicated process has).
  • If you've already started the program and would like to reattach it to another terminal, there are ways, but they're not 100% reliable. See How can I disown a running process and associate it to a new screen shell? and linked questions.
2

The job can be further controlled by sending appropriate signals (using kill command for example).

You may try this:

  • run some long running command (yes for example, as from its output we can see, that the process is running)
  • press Ctrl + Z
  • determine process pid: pgrep yes
  • resume process (equivalent to bg or fg) using: kill -CONT <PID>, where <PID> is the proces id determined in previous step
0

When you press Ctrl+Z you don't send the process to the background, you set it to sleep. You can wake it up with the fg command (foreground). Ctrl+Z sends the SIGSTOP (19) signal to the process.

You can prove this:

  • Open two terminals; in one of them type this command yes running, it will print 'running''running''running' repeatedly on the terminal
  • Stop the process with Ctrl-Z and look for its PID with the ps command.
  • Type this in the second terminal: kill -19 the_previous_PID, you'll see it produce the same effect for the 'yes' process.

If you type kill -l you'll see a list of all the signals you can use.

nopcorn
  • 9,559
FarK
  • 101