Linux Job Control Cheat Sheet: jobs, fg, bg, & wait Explained for SOC Analysts and Admins
Last verified: October 23, 2026. Examples target interactive Bash on Linux.
It's 2 a.m. and you're SSH'd into a production server during an incident response call. You started a log search that's going to take twenty minutes, and now you need a second prompt to check network connections. Open another SSH session? Cancel the search and lose your progress? Neither. Shell job control solves this in about three keystrokes, and it's one of the most underrated skills for anyone doing Linux administration, SOC investigation, or penetration testing.
This guide walks through the full job control toolkit, explains the gotchas that trip up even experienced admins, and shows how the same concepts help you spot suspicious background activity on a system you're investigating.
Table of Contents
- What Is Linux Job Control?
- Listing and Inspecting Jobs
- Running Jobs in the Background and Foreground
- Using wait, $!, and PIDs
- Output Redirection and nohup
- Controlling Jobs with Signals
- Job Control from a Security Perspective
- Expert Tips
- FAQ
What Is Linux Job Control?
A job is a process (or pipeline of processes) that your current shell is tracking. Job control is the shell feature that lets you suspend, resume, and switch those jobs between foreground and background. Per the Bash documentation, it is available in interactive shells where the monitor option is enabled.
Three ideas explain almost everything:
- Foreground job: owns your terminal. You can't type a new command until it finishes or is suspended.
- Background job: runs while you keep using the prompt.
- Stopped job: suspended (usually with Ctrl+Z), not consuming CPU until resumed.
One critical limitation: jobs, fg, and bg only work on jobs belonging to your current shell. They cannot control a process started in another terminal. For that you need PIDs and signals.
Listing and Inspecting Jobs
Start with visibility. These are the variants you'll actually use:
jobs # list jobs in the current shell
jobs -l # include process IDs
jobs -p # print only process (group leader) IDs
jobs -r # running jobs only
jobs -s # stopped jobs only
jobs -n # jobs whose status changed since last report
Expected output looks like this:
[1]- 4182 Running sleep 300 &
[2]+ 4190 Stopped vim notes.txt
The number in brackets is the job number. The + marks the current job and - the previous one. Run jobs -l before closing any terminal so you know what you're about to orphan or kill.
Running Jobs in the Background and Foreground
Starting a job in the background with &
sleep 300 &
python3 script.py --verbose &
The shell prints the job number and PID, then returns your prompt. Remember that & starts a command asynchronously; it does not guarantee the process survives when you close the terminal.
Suspending and resuming
Started something in the foreground and realized it's slow? Press Ctrl+Z to suspend it, then choose where it continues:
fg # resume the current job in the foreground
bg # resume the current stopped job in the background
Job specifiers (the % syntax)
fg %1 # job number 1
fg %+ # current job
fg %- # previous job
fg %sleep # job whose command starts with "sleep"
bg %2 # resume job 2 in the background
A frequent beginner mistake: %1 means job number 1, not PID 1. Run jobs -l to see both numbers side by side.
Using wait, $!, and PIDs in Scripts
When you automate triage tasks, such as running several evidence-collection commands in parallel, you need PIDs and exit codes:
sleep 300 &
pid=$!
echo "$pid" # PID of the most recent background command
wait "$pid" # block until that process finishes
status=$?
echo "$status" # its exit status
wait %1 # wait for job 1
wait # wait for all background children of this shell
$! expands to the PID of the most recently started asynchronous command in the current shell. wait only works for children the shell knows about, so it won't collect a process started elsewhere.
Output Redirection and nohup for Long-Running Tasks
Background jobs still write to your terminal unless you redirect them, which gets messy fast:
sleep 300 >job.log 2>&1 & # stdout+stderr to a file
command >>job.log 2>&1 & # append instead of overwrite
command >/dev/null 2>&1 & # discard all output
command <input.txt >output.txt 2>&1 & # redirect input and output
To help a job survive a hangup signal (for example, an SSH disconnect):
nohup command >nohup.out 2>&1 &
Be realistic about what nohup does: it protects against hangup signals, but it does not manage every terminal, input, or session dependency. For long investigations on remote hosts, a terminal multiplexer such as tmux or screen is usually the more dependable choice.
Controlling Jobs with Signals
When a process isn't in your shell's job table, PIDs and signals are the tool:
ps -p "$job_pid" -o pid,stat,comm # is it still running?
kill -STOP "$job_pid" # suspend
kill -CONT "$job_pid" # continue
kill -TERM "$job_pid" # polite termination request
Warning: always confirm the PID before sending a signal. On a production server, terminating the wrong process can cause an outage or destroy volatile evidence you needed for the investigation.
Handy shell introspection commands:
echo "$$" # PID of the current shell
echo "$PPID" # parent PID of the current shell
set -o | grep monitor # check whether job control is on
set -m # enable job control
set +m # disable job control (use carefully in scripts)
Job Control from a Security Perspective
Job control is a user-level convenience, but understanding it sharpens how you read a Linux host during triage.
Why background processes matter in investigations
Legitimate admins background tasks constantly. So do attackers who want a process to keep running while they continue interacting with a system. A long-lived process with no controlling terminal, an odd parent, or a strange working directory deserves a closer look. Whether that is malicious depends entirely on context, so treat it as a lead rather than a verdict.
Defensive checks worth running
ps -eo pid,ppid,tty,stat,etime,user,cmd --sort=start_time
ss -tulpn
The first command lists processes with parent, terminal, and elapsed time, which makes orphaned or oddly long-running processes easier to spot. The second shows listening sockets and the owning process (run with sufficient privileges to see all owners). Expect a table of processes and sockets; compare against what the host should normally be running.
What to pair this with
- Process auditing: Linux auditing (auditd) or an endpoint detection and response (EDR) platform can record process execution so you aren't relying on someone's shell history.
- Shell history limits: job control state lives in the shell's memory. If the shell exits, that job table is gone, so don't treat it as durable evidence.
- Baselines: knowing which scheduled and long-running processes belong on a server makes anomalies stand out quickly in enterprise SOC workflows.
No single command guarantees you'll catch malicious activity. These checks work best as one layer within a broader monitoring and incident response process.
Expert Tips
- Check before you close. Run
jobs -lbefore exiting a terminal. Stopped jobs can be terminated or leave surprises behind. - Use
waitfor exit codes when parallelizing evidence collection so a failed step doesn't go unnoticed. - Prefer tmux or screen for sessions you must be able to reattach to after a dropped connection.
- Don't assume portability. Behavior differs between shells. These examples primarily target interactive Bash, and zsh or dash may differ in details.
- Use
set -msparingly in scripts. Enabling job control in non-interactive scripts changes process-group behavior and can produce surprising results. - Read the built-in help:
help jobs,help fg,help bg, andman bash(see the JOB CONTROL section).
Quick Reference Table
| Goal | Command |
| List jobs with PIDs | jobs -l |
| Run in background | command & |
| Suspend foreground job | Ctrl+Z |
| Resume in foreground | fg %1 |
| Resume in background | bg %1 |
| Last background PID | echo "$!" |
| Wait for a job | wait %1 |
| Survive hangup | nohup command >nohup.out 2>&1 & |
| Suspend/continue by PID | kill -STOP / kill -CONT |
Related Cybersecurity Topics You Should Explore
- PHP2Shell Patch Guide: Fix CVE-2026-109618 & 109619 Fast
- FortiSIEM CVE-2026-106173: Public PoC Drops, Patch Now
- Linux renice Command: 54 Examples to Control CPU Priority
- SBI Data Breach Claim: What's Real and What's Not
- Linux nice Command: 30 Examples to Protect Your Server CPU
- iOS 27.0.1 Untethered Jailbreak Explained: Risk and Fixes
- Nextcloud Servers Backdoored: Collabora Flaw CVE-2026-110447
- Linux killall Command: 31 Examples & Safe Usage Cheat Sheet
FAQ
What is the difference between fg and bg?
fg brings a job to the foreground so it controls your terminal. bg resumes a stopped job in the background so you keep your prompt.
Does adding & keep a process alive after I close the terminal?
Not reliably. The ampersand only runs the command asynchronously. Use nohup, a terminal multiplexer, or a service manager for persistence.
Why does fg say "no such job"?
The job doesn't belong to your current shell, or it has already finished. Run jobs -l to see what the shell is tracking.
Is %1 the same as PID 1?
No. %1 is job number 1 in your shell's job table. PID 1 is the init/system manager process.
Can I control a job started in another terminal with bg or fg?
No. Those commands only act on jobs known to the current shell. Use PIDs with signals such as kill -STOP and kill -CONT instead.
What does $! return?
The PID of the most recently started asynchronous command in the current shell.
Conclusion
Job control isn't glamorous, but it's the difference between fumbling through a second SSH session and calmly pausing, backgrounding, and resuming work mid-investigation. Master jobs, fg, bg, wait, and the % specifiers, understand the limits of & and nohup, and you'll also read process activity on unfamiliar Linux hosts with more confidence. Practice these on a test machine first, and keep help jobs a keystroke away.
Analysis based on SOC monitoring experience and review of public Bash documentation.
