Skip to main content

Job Control Cheatsheet

By Dejan Panovski •Updated on • Download PDF

Quick reference for Bash job control: background jobs, jobs, fg, bg, wait, and keeping processes alive after logout with disown, nohup, and setsid

Job control lets you run commands in the background, suspend and resume them, and move them between the foreground and background of the current shell. This cheatsheet covers &, jobs, fg, bg, kill, wait, disown, and nohup as they work in Bash.

Run in the Background

End a command with & to get the prompt back right away.

CommandDescription
command &Run a command in the background
command > out.log 2>&1 &Background job with output saved to a file
command > /dev/null 2>&1 &Background job with all output discarded
(cmd1; cmd2) &Run a command group as one job
cmd1 & cmd2 &Start two jobs in parallel
echo $!Print the PID of the last background job

List Jobs

jobs shows only jobs started from the current shell. Use ps for processes from other sessions.

CommandDescription
jobsList jobs with their state
jobs -lInclude process IDs
jobs -pPrint only process group leader PIDs
jobs -rList running jobs only
jobs -sList stopped jobs only
jobs -nList jobs whose state changed since the last notice
jobs %2Show job 2 only

Job Specifications

fg, bg, kill, wait, and disown accept these job references.

JobspecDescription
%1Job number 1
%% or %+Current job, marked + in jobs
%-Previous job, marked - in jobs
%pingJob whose command starts with ping
%?logJob whose command contains log

Without a jobspec, fg, bg, and disown act on the current job.

Foreground and Background

Suspend a running command, then decide where it continues.

CommandDescription
Ctrl+ZSuspend the foreground job (SIGTSTP)
bgResume the current job in the background
bg %2Resume job 2 in the background
fgBring the current job to the foreground
fg %1Bring job 1 to the foreground
%1Short form of fg %1
%1 &Short form of bg %1

Stop and Kill Jobs

kill accepts a jobspec as well as a PID.

CommandDescription
Ctrl+CInterrupt the foreground job (SIGINT)
Ctrl+\Quit the foreground job (SIGQUIT)
kill %1Send SIGTERM to job 1
kill -9 %1Force kill job 1 (SIGKILL)
kill -STOP %1Pause job 1; the signal cannot be caught
kill -CONT %1Continue a stopped job in the background
kill $(jobs -p)Send SIGTERM to every job in the current shell

Wait for Jobs

Use wait in scripts to block until background work finishes.

CommandDescription
waitWait for all background jobs
wait %1Wait for job 1
wait "$pid"; echo $?Wait for a PID and print its exit status
wait -nWait for the next job to finish
wait -n -p idStore the PID of the finished job in id (Bash 5.1+)
for h in web1 web2; do ssh "$h" uptime & done; waitRun commands in parallel, then wait for all

Keep Jobs Running After Logout

Redirect output before you log out. A job that keeps writing to a closed terminal can fail.

CommandDescription
disown %1Remove job 1 from the job table
disown -h %1Keep job 1 listed but do not send it SIGHUP on exit
disown -aRemove all jobs
disown -rRemove running jobs only
Ctrl+Z, bg, disown -hMove a running foreground command to the background and protect it
nohup command &Ignore SIGHUP; output goes to nohup.out
nohup command > out.log 2>&1 &Ignore SIGHUP and write output to a named log
setsid -f command > out.log 2>&1 < /dev/nullStart in a new session, detached from the terminal

Job States

States shown by jobs and the shell’s job notices.

StateDescription
RunningJob runs in the background
StoppedJob is suspended and waits for fg or bg
Stopped (tty input)Background job tried to read from the terminal; use fg
DoneJob finished with exit status 0
Exit 3Job finished with a non-zero exit status
TerminatedJob ended on SIGTERM
KilledJob ended on SIGKILL

Shell Options

Settings that change how Bash handles jobs.

CommandDescription
set -mEnable job control in a script (on by default in interactive shells)
set -bReport finished jobs immediately instead of at the next prompt
shopt -s checkjobsList running and stopped jobs before the shell exits
shopt -s huponexitSend SIGHUP to all jobs when an interactive login shell exits
stty tostopStop background jobs that write to the terminal
stty -tostopLet background jobs write to the terminal (default)

Use these guides for the longer explanations behind these commands.

GuideDescription
How to Run Linux Commands in the Background&, disown, nohup, and setsid in detail
jobs Command in LinuxJob specifications, states, and options
nohup Command in Linuxnohup.out, redirection, and logout behavior
How to Use the kill Command in LinuxSignals and how to send them
Bash wait CommandExit status handling for background jobs
tmux CheatsheetSessions you can detach and reattach later
screen CheatsheetThe same idea with GNU Screen