Job Control Cheatsheet
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.
| Command | Description |
|---|---|
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.
| Command | Description |
|---|---|
jobs | List jobs with their state |
jobs -l | Include process IDs |
jobs -p | Print only process group leader PIDs |
jobs -r | List running jobs only |
jobs -s | List stopped jobs only |
jobs -n | List jobs whose state changed since the last notice |
jobs %2 | Show job 2 only |
Job Specifications
fg, bg, kill, wait, and disown accept these job references.
| Jobspec | Description |
|---|---|
%1 | Job number 1 |
%% or %+ | Current job, marked + in jobs |
%- | Previous job, marked - in jobs |
%ping | Job whose command starts with ping |
%?log | Job 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.
| Command | Description |
|---|---|
Ctrl+Z | Suspend the foreground job (SIGTSTP) |
bg | Resume the current job in the background |
bg %2 | Resume job 2 in the background |
fg | Bring the current job to the foreground |
fg %1 | Bring job 1 to the foreground |
%1 | Short form of fg %1 |
%1 & | Short form of bg %1 |
Stop and Kill Jobs
kill accepts a jobspec as well as a PID.
| Command | Description |
|---|---|
Ctrl+C | Interrupt the foreground job (SIGINT) |
Ctrl+\ | Quit the foreground job (SIGQUIT) |
kill %1 | Send SIGTERM to job 1 |
kill -9 %1 | Force kill job 1 (SIGKILL) |
kill -STOP %1 | Pause job 1; the signal cannot be caught |
kill -CONT %1 | Continue 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.
| Command | Description |
|---|---|
wait | Wait for all background jobs |
wait %1 | Wait for job 1 |
wait "$pid"; echo $? | Wait for a PID and print its exit status |
wait -n | Wait for the next job to finish |
wait -n -p id | Store the PID of the finished job in id (Bash 5.1+) |
for h in web1 web2; do ssh "$h" uptime & done; wait | Run 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.
| Command | Description |
|---|---|
disown %1 | Remove job 1 from the job table |
disown -h %1 | Keep job 1 listed but do not send it SIGHUP on exit |
disown -a | Remove all jobs |
disown -r | Remove running jobs only |
Ctrl+Z, bg, disown -h | Move 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/null | Start in a new session, detached from the terminal |
Job States
States shown by jobs and the shell’s job notices.
| State | Description |
|---|---|
Running | Job runs in the background |
Stopped | Job is suspended and waits for fg or bg |
Stopped (tty input) | Background job tried to read from the terminal; use fg |
Done | Job finished with exit status 0 |
Exit 3 | Job finished with a non-zero exit status |
Terminated | Job ended on SIGTERM |
Killed | Job ended on SIGKILL |
Shell Options
Settings that change how Bash handles jobs.
| Command | Description |
|---|---|
set -m | Enable job control in a script (on by default in interactive shells) |
set -b | Report finished jobs immediately instead of at the next prompt |
shopt -s checkjobs | List running and stopped jobs before the shell exits |
shopt -s huponexit | Send SIGHUP to all jobs when an interactive login shell exits |
stty tostop | Stop background jobs that write to the terminal |
stty -tostop | Let background jobs write to the terminal (default) |
Related Guides
Use these guides for the longer explanations behind these commands.
| Guide | Description |
|---|---|
How to Run Linux Commands in the Background | &, disown, nohup, and setsid in detail |
jobs Command in Linux | Job specifications, states, and options |
nohup Command in Linux | nohup.out, redirection, and logout behavior |
How to Use the kill Command in Linux | Signals and how to send them |
Bash wait Command | Exit status handling for background jobs |
tmux Cheatsheet | Sessions you can detach and reattach later |
screen Cheatsheet | The same idea with GNU Screen |