Unix - What is Job Control?
1. What is Job Control?
Job control allows you to manage multiple processes (jobs) started from the same shell. You can:
-
Pause (stop) a job
-
Resume it in the foreground or background
-
List running/stopped jobs
2. Important Commands
a) & (Run in Background)
-
Appending
&to a command sends it to the background immediately. -
The shell prints the job number and process ID (PID).
Example:
$ sleep 60 &
[1] 2345
-
1= job number -
2345= PID of the process
b) jobs (List Jobs)
-
Shows all jobs started in the current shell, along with their status:
-
Running= process is active -
Stopped= process paused
-
Example:
$ jobs
[1]+ Running sleep 60 &
[2]- Stopped vim file.txt
c) fg (Bring Job to Foreground)
-
Brings a background or stopped job to the foreground so you can interact with it.
Syntax:
fg %[job_number]
Example:
$ fg %1
-
Job
1now runs in the foreground.
d) bg (Resume Job in Background)
-
Resumes a stopped job in the background.
Syntax:
bg %[job_number]
Example:
$ bg %2
-
Job
2will now continue running in the background.
3. Pausing and Resuming Jobs
-
Ctrl + Z: Pause the current foreground job and move it to the background (stopped).
-
Then you can use
bgto resume it in the background orfgto resume in foreground.
Example Workflow:
$ vim file.txt # open vim
# press Ctrl+Z
[1]+ Stopped vim file.txt
$ bg %1 # resume in background
[1]+ vim file.txt &
$ fg %1 # bring back to foreground
4. Quick Tips
-
%= prefix to specify job number (fromjobscommand) -
Use
jobs -lto also see the PID of each job -
You can have multiple jobs running simultaneously in the background
Summary Table
| Command | Function |
|---|---|
command & |
Run command in background |
jobs |
List all jobs |
fg %n |
Bring job n to foreground |
bg %n |
Resume stopped job n in background |
Ctrl + Z |
Pause current foreground job |
If you want, I can draw a small diagram showing jobs moving between foreground, background, stopped, and running states. This makes it really easy to visualize.