Linux Labs

Users, permissions, and processes / Stage 11 of 13

Background jobs and the terminal

Not running Starting usually ~20 s
Leave the lesson

Step 4 of 7 · theory · 4 min

Job number and PID

When there are several jobs, you address the one you need by its number, written with a percent sign.

fg %2 brings job 2 to the foreground, and bg %1 continues job 1 in the background. Without a number, fg and bg take the current job, the one jobs marks with +. The - sign marks the previous one.

fg and bg understand a number without % too, but other commands take such a number for a PID. kill, which sends signals to processes, takes kill 1 to mean process 1, which is systemd, and is refused, while kill %1 ends job 1. That is why you always write a job number with %.

Check yourself 1 question

Which command continues the stopped job in the background?

$ jobs -l
[1]-  2034 Running                 sleep 600 &
[2]+  2041 Stopped                 sleep 700
  • A job number goes after %, and 2041 is a PID. There is no job 2041, so bg prints no such job.

  • Job 1 is already Running. The stopped one is job 2: [2]+ … Stopped.

  • fg continues job 2 in the foreground and takes the terminal. bg is what continues it in the background.

Job 2 is the stopped one, and bg %2 continues it in the background. The % goes before a job number; a PID is written without it.

Key points

  • A job number is written with %: without it, kill and other commands take the number for a PID.
  • fg %N and bg %N continue job N in the foreground or in the background; without a number they take the current job, marked +.
Ubuntu 24.04
Loading…