The jobs, & and nohup command in Linux
The shell can run several things at once: append & to launch in the background, Ctrl+Z to suspend the foreground task, jobs to list them, fg/bg to bring back or resume. For work that must outlive your SSH session, nohup (or better, tmux) detaches it from the terminal's fate. Five keystrokes of job control replace many opened terminals.
How jobs, & and nohup works
Job control rests on process groups and sessions: every pipeline you launch becomes a process group, your terminal session owns them all, and exactly one group is "foreground" — the one receiving your keystrokes and your Ctrl+C. & starts a group in the background; Ctrl+Z sends SIGTSTP to suspend the foreground group; fg and bg move groups between states. jobs is the shell's ledger of its groups.
The logout question is a signal story: when the terminal disappears, the session leader (your shell) receives SIGHUP and forwards it to its jobs — default action, death. nohup arranges immunity in advance (ignore SIGHUP, redirect output away from the doomed terminal); disown strikes a job from the shell's ledger retroactively. tmux solves it structurally: the session belongs to a server, and your terminal is merely a detachable window into it.
Syntax
CMD & | jobs | fg %N | bg %N | nohup CMD & Common options
| Option | What it does |
|---|---|
cmd & | Start in the background; the prompt returns immediately. |
Ctrl+Z | Suspend the current foreground job. |
jobs | List this shell's jobs with their %numbers. |
fg %1 / bg %1 | Foreground / resume-in-background job 1. |
nohup cmd & | Immune to hangup when you log out; output to nohup.out. |
disown %1 | Detach an ALREADY-running job from the shell. |
How to use jobs, & and nohup: examples
$ ./long-build.sh > build.log 2>&1 & Fire and keep working; check the log at will.
$ jobs [1]+ Running ./long-build.sh & — your background inventory.
$ fg %1 Bring it back to interact; Ctrl+Z + bg sends it away again.
$ nohup ./migrate.sh > migrate.log 2>&1 & The over-SSH long task: survives your disconnection.
$ disown -h %1 Forgot nohup? Detach retroactively before logging out.
Real-world use cases for jobs, & and nohup
The editor juggle
Deep in vim, need the shell: Ctrl+Z, run your commands, fg — back exactly where you were. One terminal, instant switching; the workflow that makes job control worth learning in year one.
The long remote task
A 3-hour migration over hotel Wi-Fi: tmux new, run it, detach — or nohup ./migrate.sh > log 2>&1 & if tmux is missing. Either way, your connection dying no longer kills the work.
Pro tips and common mistakes
- Redirect background jobs at launch (> log 2>&1 &) or their output interleaves into your prompt.
- Forgot nohup on a running job? Ctrl+Z, bg, disown -h — rescued retroactively.
- Learn tmux beyond the basics; nohup is the patch, tmux is the cure.
Frequently asked questions about jobs, & and nohup
Why did my background task die when I logged out?
On logout the terminal sends SIGHUP to its jobs. nohup shields from it in advance; disown removes running jobs from the list. For real work sessions, tmux/screen are the professional answer: reattachable terminals.
Where does background output go?
Still to your terminal, interleaved annoyingly — redirect at launch: cmd > out.log 2>&1 &.
Job numbers vs PIDs?
%1 is this shell's bookkeeping; the PID is system-wide. kill accepts both: kill %1 or kill 12345.
Related commands
Log into remote machines, run remote commands, use keys instead of passwords, and tunnel ports.
psSee what is running, find a process ID, and combine with grep to hunt down a program.
killTerminate processes politely or forcibly — and understand what -9 really does.