Computing and the Command Line

Jobs and Signals

Run a command in the background with &, pause the foreground job with Ctrl-Z, and move jobs between foreground and background with fg and bg. Signals are how processes are told to stop: INT from Ctrl-C, TERM from kill, KILL as a last resort, HUP when the terminal closes, and TSTP from Ctrl-Z. kill, killall, and pkill; nohup to survive closing the terminal; nice and renice for priority; and shutting down cleanly.

  • 6 min
  • 8 steps
  • 3 questions
  • Lesson 17 of 80

In this lesson

  1. Background jobs
  2. Signals
  3. kill, killall, and pkill
  4. Surviving a closed terminal
  5. Priority: nice and renice
  6. Shutting down
  7. Your turn
  8. So

Background jobs

Normally a command runs in the foreground: it has the terminal, and you wait for the prompt to come back. Put & at the end and it runs in the background, and you get the prompt back at once 1:

me@linuxbox:~$ sleep 600 &
[1] 2381
me@linuxbox:~$ jobs
[1]+  Running                 sleep 600 &

The shell prints a job number in brackets and the process ID. jobs lists the jobs started from this shell 1.

Three keys and commands move jobs around 1:

  • Ctrl-Z pauses (stops) the foreground job and gives you the prompt.
  • bg resumes a stopped job in the background.
  • fg brings a job to the foreground. Pick one with its job number: fg %1.
me@linuxbox:~$ rsync -a ~/photos /mnt/backup/      # oops, this will take a while
^Z
[1]+  Stopped                 rsync -a ~/photos /mnt/backup/
me@linuxbox:~$ bg
[1]+ rsync -a ~/photos /mnt/backup/ &
me@linuxbox:~$                                       # prompt back; rsync carries on

A background job still prints to your terminal, which can be messy; redirect its output to a file when you start it: long-job > job.log 2>&1 &. And a background job that tries to read from the keyboard is stopped until you bring it to the foreground 1.

Three boxes. Running in the foreground has the terminal. Ctrl-Z moves it to stopped, paused. bg resumes a stopped job running in the background, where you keep typing; fg brings a stopped job back to the foreground, and fg %1 brings a background job to the foreground. A command ending in & starts straight in the background. From any state, Ctrl-C (in the foreground) or kill %1 or kill PID ends it, and jobs lists them. Below, signals: INT 2, Ctrl-C, interrupt; TERM 15, kill's default, please stop; KILL 9, the kernel ends it now with no clean-up; HUP 1, the terminal closed, or reload for daemons; TSTP 20, Ctrl-Z, pause; STOP and CONT, 19 and 18, pause and resume and can't be ignored.
Three states, a few keys, and the signals behind them. Credit: StudyCorner diagram · CC BY 4.0 · Source

Quick check

You started a long download in the foreground and want your prompt back without stopping it. What do you press and type?

Signals

How do you tell a program to stop? You send it a signal, a small message the kernel delivers. Ctrl-C and Ctrl-Z are signals; so is everything kill sends 1. A program can catch most signals and react, for example by saving its work before quitting.

The signals worth knowing 1:

Signal Number Sent by Means
INT 2 Ctrl-C Interrupt; usually ends the program
TERM 15 kill (default) Please terminate
KILL 9 kill -9 The kernel ends it immediately; it can’t be caught or ignored
HUP 1 closing the terminal Hang up; daemons often reload their settings on HUP
TSTP 20 Ctrl-Z Pause from the terminal; can be ignored
STOP, CONT 19, 18 kill -STOP, fg/bg Pause and resume; STOP can’t be ignored

kill -l lists them all.

Quick check

A program won’t quit. In what order should you try signals?

kill, killall, and pkill

kill sends a signal to a process ID or a job number 1:

me@linuxbox:~$ kill 2381          # TERM: please stop
me@linuxbox:~$ kill %1            # the same, by job number
me@linuxbox:~$ kill -9 2381       # KILL: last resort
me@linuxbox:~$ kill -HUP 812      # signals by name or number

Always try plain kill first. kill -9 gives the program no chance to save, clean up temporary files, or finish writing, so it’s for programs that ignore TERM 1.

To signal programs by name 1:

me@linuxbox:~$ killall firefox        # every process named exactly firefox
me@linuxbox:~$ pkill -f backup.sh     # every process whose command line contains backup.sh
me@linuxbox:~$ pgrep -a python        # see what pkill would match, first

You can signal only your own processes; other users’ need sudo 1. In top and htop, k sends a signal too.

Surviving a closed terminal

Closing a terminal sends HUP to the jobs running in it, and most programs quit on HUP 1. To keep a long job alive after you close the window, start it with nohup, which makes it ignore HUP 1:

me@linuxbox:~$ nohup ./big-backup.sh > backup.log 2>&1 &

(Without your own redirection, nohup saves the output to nohup.out.) For anything you’ll want to come back to and watch, a terminal multiplexer like tmux is better: it keeps whole sessions alive and lets you reattach, even over SSH. The Linux course covers it.

Quick check

Why does a program started from a terminal usually die when you close that terminal?

Priority: nice and renice

Each process has a niceness from -20 (greediest) to 19 (most polite); the default is 0. A nicer process gets the CPU only after less nice ones. Ordinary users can only make their own processes nicer; only root can raise priority 1:

me@linuxbox:~$ nice -n 19 tar czf archive.tar.gz ~/photos    # start it polite
me@linuxbox:~$ renice -n 19 -p 2381                          # make a running one polite

Niceness 19 means “only when nothing else wants the CPU”: perfect for big compressions and video conversions while you keep working 1.

Shutting down

Shutting down stops every process in order and makes sure everything is written to disk 1:

me@linuxbox:~$ sudo shutdown -h now       # power off
me@linuxbox:~$ sudo reboot                # restart

Under WSL, there’s nothing to power off: close the window, or from PowerShell run wsl --shutdown to stop all distributions.

Your turn

Exercises

  1. Start sleep 1000 & twice. List the jobs, bring the first to the foreground, pause it with Ctrl-Z, and send it back to the background.
  2. Kill both sleeps: one with kill %N, one with kill and its PID.
  3. Start sleep 1000, then in another tab, end it with pkill sleep. What did the first tab print?
  4. Run yes > /dev/null & (a program that burns a whole CPU core), watch it in top, renice it to 19, and kill it.
  5. Which signal does kill send if you don’t name one? Check with man kill.
Answers
  1. jobs shows [1] and [2]; fg %1, Ctrl-Z, then bg %1.
  2. kill %2; for the other, jobs -l shows its PID, then kill <PID>.
  3. Terminated. pkill sent TERM.
  4. In top it sits near 100% CPU; renice -n 19 -p <PID> changes its NI column to 19; kill %1 (or kill <PID>) ends it.
  5. TERM (15).

So

& starts a job in the background; Ctrl-Z pauses the foreground one, and bg and fg move jobs between the two. Programs are stopped with signals: Ctrl-C sends INT, kill sends TERM, and kill -9 sends KILL only as a last resort. nohup survives a closed terminal, and nice keeps big jobs out of your way.

Lesson complete

Nice work.

1day streak
0/1today's goal
–correct

Up next · 5 min

Exit Status and Chaining

Next lesson
Sources for this lesson
  1. 1
    William Shotts. The Linux Command Line, Seventh Internet Edition (25.12A). LinuxCommand.org (print edition by No Starch Press). 2026. verifiedFree CC BY-NC-ND 3.0 book, release 25.12A of July 18, 2026. Part 1, Learning the Shell: the shell and terminal emulators, prompts ($ vs. # for the superuser), command history (most distributions keep the last 1,000 commands), Shift-Ctrl-C/V for copy and paste; navigation and the directory tree; exploring the system (ls options and the long listing, file, less, the guided tour of /, symbolic links); manipulating files (wildcards and character classes, mkdir, cp, mv, rm, ln; no undelete, test wildcards with ls first); working with commands (four kinds of commands, type, which, help, --help, man and its sections, apropos, whatis, info, alias); redirection; expansion and quoting; Readline keyboard tricks, completion, history search; permissions; processes. Later parts cover the environment, vi, packages, storage, networking, find, archiving, regular expressions, text processing, and shell scripting.