CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Process Management / jobs

jobs

List the shell's background and stopped jobs

Beginnerjob controlshell

jobs lists the commands your current shell has running in the background or stopped with Ctrl-Z, each with a job number like %1. Those job numbers are what fg, bg, kill and wait use, and they only exist inside the shell that started them.

Syntaxjobs [-l | -p] [-r | -s] [%JOB...]

Watch it run

You kick off a database export and a compression in the background and want to keep an eye on both.

devops@devops-lab: ~/data — zsh — 96×28
➜ ~/data
Step 1 of 5
./export.sh > export.log 2>&1 &

The trailing & starts the command as background job 1. bash prints the job number in brackets and the PID, and gives you the prompt back at once.

Anatomy of a command

$
commandjobs

A bash builtin: it reads the shell's own job table. There is no /usr/bin/jobs to call from other programs.

How it works

Job control is a feature of an interactive shell plus the terminal driver. When you run a pipeline, bash puts all its processes in a new process group (setpgid). For a foreground job it hands the terminal to that group with tcsetpgrp, so keyboard signals (Ctrl-C sends SIGINT, Ctrl-Z sends SIGTSTP) go to that group, not to the shell. A command ending in & stays in its own group in the background while bash keeps the terminal. bash records every group in its job table with a number, the command text and a state, and updates the state when waitpid tells it a child stopped, continued or exited. jobs just prints that table. + marks the current job (the default for fg and bg) and - the previous one. Another terminal, a cron job or a script without set -m has no job table to show.

The terminal driver signals only the foreground process group; bash moves jobs between foreground, background and stopped.

keysSIGINT/TSTPCtrl-Zbg: SIGCONTcmd &run cmdfgKeyboardCtrl-C, Ctrl-ZTTY driverfg pgrp = 2412bashjob tableForegroundowns terminaltar czf ...Stoppedstate TBackgroundruns, no stdin
1/7
Run a command in the foreground

bash puts the new process in its own process group and hands the terminal to that group with tcsetpgrp. The TTY now delivers keyboard signals to it.

← → to step

Options you'll actually use

-lInclude the PID of each job
-pPrint only the PIDs (handy for kill $(jobs -p))
-rOnly running jobs
-sOnly stopped jobs
%NJob number N
%+ / %%The current job (marked +)
%-The previous job (marked -)
%string / %?stringThe job whose command starts with / contains string

Common mistakes

Running jobs in a new terminal to find a job you started elsewhere

The job table belongs to the shell that started the job. Other shells can't see or use its %N numbers.

pgrep -af NAME, then manage it by PID
Using kill %1 inside a cron script

Non-interactive shells don't enable job control by default, so job specs and fg/bg don't behave as in a terminal.

Save $! after starting the process: cmd & pid=$!; kill $pid
Leaving Ctrl-Z'd jobs behind and forgetting them

Stopped processes hold memory, file handles, locks and listening ports; a stopped vim keeps its .swp file and a stopped server still owns its port.

jobs -s before logging out; fg and quit them properly

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Start `sleep 300 &` and `sleep 400 &`, then list your jobs with their PIDs.

  2. 2

    Run `sleep 500` in the foreground, stop it with Ctrl-Z, and list only stopped jobs.

  3. 3

    Kill all your background jobs in one command using their PIDs.

Related commands

bgResume a stopped job in the backgroundfgBring a job back to the foregroundkillSend a signal to a processnohupKeep a command running after you log out
PreviouspkillNextbg
Linux / Process Management / jobs

jobs

List the shell's background and stopped jobs

Beginnerjob controlshell

jobs lists the commands your current shell has running in the background or stopped with Ctrl-Z, each with a job number like %1. Those job numbers are what fg, bg, kill and wait use, and they only exist inside the shell that started them.

Syntaxjobs [-l | -p] [-r | -s] [%JOB...]

Watch it run

You kick off a database export and a compression in the background and want to keep an eye on both.

devops@devops-lab: ~/data — zsh — 96×28
➜ ~/data
Step 1 of 5
./export.sh > export.log 2>&1 &

The trailing & starts the command as background job 1. bash prints the job number in brackets and the PID, and gives you the prompt back at once.

Anatomy of a command

$
commandjobs

A bash builtin: it reads the shell's own job table. There is no /usr/bin/jobs to call from other programs.

How it works

Job control is a feature of an interactive shell plus the terminal driver. When you run a pipeline, bash puts all its processes in a new process group (setpgid). For a foreground job it hands the terminal to that group with tcsetpgrp, so keyboard signals (Ctrl-C sends SIGINT, Ctrl-Z sends SIGTSTP) go to that group, not to the shell. A command ending in & stays in its own group in the background while bash keeps the terminal. bash records every group in its job table with a number, the command text and a state, and updates the state when waitpid tells it a child stopped, continued or exited. jobs just prints that table. + marks the current job (the default for fg and bg) and - the previous one. Another terminal, a cron job or a script without set -m has no job table to show.

The terminal driver signals only the foreground process group; bash moves jobs between foreground, background and stopped.

keysSIGINT/TSTPCtrl-Zbg: SIGCONTcmd &run cmdfgKeyboardCtrl-C, Ctrl-ZTTY driverfg pgrp = 2412bashjob tableForegroundowns terminaltar czf ...Stoppedstate TBackgroundruns, no stdin
1/7
Run a command in the foreground

bash puts the new process in its own process group and hands the terminal to that group with tcsetpgrp. The TTY now delivers keyboard signals to it.

← → to step

Options you'll actually use

-lInclude the PID of each job
-pPrint only the PIDs (handy for kill $(jobs -p))
-rOnly running jobs
-sOnly stopped jobs
%NJob number N
%+ / %%The current job (marked +)
%-The previous job (marked -)
%string / %?stringThe job whose command starts with / contains string

Common mistakes

Running jobs in a new terminal to find a job you started elsewhere

The job table belongs to the shell that started the job. Other shells can't see or use its %N numbers.

pgrep -af NAME, then manage it by PID
Using kill %1 inside a cron script

Non-interactive shells don't enable job control by default, so job specs and fg/bg don't behave as in a terminal.

Save $! after starting the process: cmd & pid=$!; kill $pid
Leaving Ctrl-Z'd jobs behind and forgetting them

Stopped processes hold memory, file handles, locks and listening ports; a stopped vim keeps its .swp file and a stopped server still owns its port.

jobs -s before logging out; fg and quit them properly

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Start `sleep 300 &` and `sleep 400 &`, then list your jobs with their PIDs.

  2. 2

    Run `sleep 500` in the foreground, stop it with Ctrl-Z, and list only stopped jobs.

  3. 3

    Kill all your background jobs in one command using their PIDs.

Related commands

bgResume a stopped job in the backgroundfgBring a job back to the foregroundkillSend a signal to a processnohupKeep a command running after you log out
PreviouspkillNextbg