Foreground, background, and surviving a logout with nohup

Video 125 of 140 in Virtualization Foundations, in the unit “Processes, signals, /proc and /sys”. 3:41, with English and Hindi audio.

A long export can take an hour, and you may need your terminal for other work while it runs. This article explains how a command can run in the foreground or the background, how to pause, list and resume jobs with the shell, and how nohup keeps a job alive after you log out. It also covers the mistakes that cost the most, such as lost output and a job that you forgot was running.

What you will learn

An export that takes an hour

An engineer starts a large export on a remote server, using a secure shell connection from a laptop. The export takes about an hour. If it runs in the foreground, the terminal waits for it, and no other command can be typed. The engineer also wants to log out, and keep the export running.

Foreground and background

A foreground job holds the terminal until it finishes. A background job runs on its own, and the prompt comes back at once. The shell is the program that reads your commands, and it is the one that starts, pauses and tracks the jobs.

A job started in the foreground holds the terminal. Press Control Z, and the job pauses and the prompt comes back. The command bg restarts that paused job in the background, so it keeps going while you type other commands. To start a job in the background from the beginning, put an ampersand at the end of the command.

The jobs command lists the jobs of this shell, with their job numbers and whether they are running or stopped. The commands bg and fg move a job between the background and the foreground, and the number after the percent sign is the job number.

List the jobs of this shell, with their numbers and states
jobs

Keeping a job alive after logout with nohup

When a session ends, the shell usually sends a hangup signal to its jobs, and the export stops with it. The nohup command ignores that signal, so the job keeps going. The word stands for no hangup. Send the output to a log file, so that nothing is lost on the screen.

Run the export so it survives logout, writing its output to export.log
nohup ./export-orders.sh > export.log 2>&1 &

Note: nohup protects against logout, but a reboot still ends the job. Keep its output in a file, so you can see how far it got.

What goes wrong with background jobs

Try it yourself

You need a Linux terminal and a script that runs for a while. The steps use export-orders.sh. If you do not have that script, use any script of your own that takes a minute or more.

  1. Run the script in the foreground. The prompt waits until it ends. Then press Control Z to pause it, so the prompt comes back.

    ./export-orders.sh

    What you should see: The prompt does not return until the script ends, unless you pause it.

  2. List the jobs of this shell, with their job numbers and states.

    jobs

    What you should see: The list shows each job with its number, and whether it is running or stopped.

  3. Resume the paused job in the background, so the prompt is free for other commands.

    bg
  4. Bring job number 1 back to the foreground.

    fg %1
  5. Start the script so that it survives a logout. It ignores the hangup signal, and its output goes to export.log.

    nohup ./export-orders.sh > export.log 2>&1 &

    What you should see: The script runs in the background, and its output is written to export.log.

Terms

Background job
A command that runs while the terminal gives you the prompt back, and that is still tied to the shell that started it.
Hangup signal
The signal the shell usually sends to its jobs when a session ends, which nohup makes a job ignore.

Check yourself

What does Control Z do to a running job?

It pauses the job, and the prompt comes back. The job can then be resumed with bg or fg.

Why does nohup keep a job alive after logout?

It ignores the hangup signal that the shell sends to its jobs when a session ends.

Why send a background job's output to a file?

Without a log file, the output goes to the screen and can be lost when the window closes.

Previous: Killing a process safely. Next: Zombie and orphan processes.