Unix - Job Control in UNIX
Job control is a feature in UNIX that allows users to manage multiple processes from a single terminal session. It enables users to run programs in the foreground or background, pause running programs, resume them later, and switch between different tasks without closing the terminal. Job control is particularly useful for system administrators, developers, and users who often work with multiple commands simultaneously.
Understanding Jobs and Processes
A process is an instance of a running program. When a command is executed in UNIX, the operating system creates a process to perform the requested task. A job is a collection of one or more processes that are managed together by the shell.
For example, when a user runs a text editor, compiler, or file-copying command, UNIX creates a process for that task. The shell keeps track of these processes and assigns them job numbers.
Foreground Processes
A foreground process runs directly in the terminal and interacts with the user. While a process is running in the foreground, the terminal is occupied, and the user cannot execute other commands until the process completes or is suspended.
Example:
cat largefile.txt
The command runs in the foreground, displaying content on the screen. The terminal remains occupied until the operation finishes.
Characteristics of foreground processes:
-
Receive keyboard input directly.
-
Display output immediately on the terminal.
-
Prevent other commands from being entered until completion.
-
Suitable for interactive programs.
Background Processes
A background process runs independently of the terminal, allowing the user to continue using the shell while the process executes.
To start a command in the background, add an ampersand (&) at the end of the command.
Example:
find / -name "*.log" &
Output:
[1] 2456
Here:
-
[1]represents the job number. -
2456represents the process ID (PID).
Benefits of background processing include:
-
Running lengthy tasks without blocking the terminal.
-
Performing multiple operations simultaneously.
-
Improving productivity by allowing continued shell usage.
Suspending a Running Process
A foreground process can be temporarily stopped without terminating it.
The most common method is pressing:
Ctrl + Z
Example:
vi report.txt
After pressing Ctrl + Z:
[1]+ Stopped vi report.txt
The process remains in memory but is not actively running.
Advantages of suspending processes:
-
Temporarily freeing the terminal.
-
Switching to another task.
-
Resuming work later without restarting the application.
Viewing Current Jobs
The jobs command displays all jobs associated with the current shell session.
Example:
jobs
Output:
[1]+ Stopped vi report.txt
[2]- Running backup.sh &
The output shows:
-
Job number.
-
Job status.
-
Command being executed.
Job status may include:
-
Running
-
Stopped
-
Done
-
Terminated
Resuming Jobs in the Background
A suspended process can be resumed in the background using the bg command.
Example:
bg %1
Output:
[1]+ vi report.txt &
The job resumes execution without taking control of the terminal.
This feature is useful when:
-
A process was accidentally started in the foreground.
-
Long-running tasks need to continue while other work is performed.
Bringing Jobs to the Foreground
The fg command moves a background or suspended job back to the foreground.
Example:
fg %1
The process regains terminal control and can interact with the user.
Typical uses include:
-
Returning to a text editor.
-
Monitoring interactive applications.
-
Continuing a paused task.
Job Identification
Each job is assigned a job number by the shell.
Examples:
jobs
Output:
[1] Running script.sh &
[2] Stopped editor.txt
Reference jobs using:
%1
%2
Examples:
fg %2
bg %1
kill %2
This makes job management simpler than remembering process IDs.
Terminating Jobs
Jobs can be terminated using the kill command.
Example:
kill %1
Or using the process ID:
kill 2456
If a process does not respond, a stronger signal can be used:
kill -9 2456
Care should be taken when using forceful termination because it may not allow the application to save data or perform cleanup operations.
Difference Between Jobs and Processes
| Feature | Job | Process |
|---|---|---|
| Managed by | Shell | Kernel |
| Identification | Job Number | Process ID (PID) |
| Scope | Current shell session | Entire system |
| Commands Used | jobs, fg, bg | ps, kill, top |
| Visibility | Shell-specific | System-wide |
A job may contain one or more processes, especially when pipelines are involved.
Job Control with Pipelines
UNIX treats an entire pipeline as a single job.
Example:
cat file.txt | grep error | sort
Although multiple processes are involved, the shell manages them as one job.
The entire pipeline can be suspended, resumed, or moved between foreground and background as a unit.
Monitoring Running Processes
Several commands help monitor processes and jobs.
Using ps
ps
Displays currently running processes.
Using top
top
Shows real-time system activity and process information.
Using pstree
pstree
Displays process relationships in a tree structure.
These tools complement job-control commands by providing system-wide process information.
Practical Example
Suppose a user starts a large backup operation:
tar -czf backup.tar.gz /home
The process takes a long time.
Step 1: Suspend the process.
Ctrl + Z
Step 2: Move it to the background.
bg
Step 3: Continue working in the terminal.
ls
pwd
Step 4: Check job status.
jobs
Step 5: Bring it back to the foreground if needed.
fg
This workflow demonstrates the flexibility of UNIX job control.
Advantages of Job Control
-
Enables multitasking within a single terminal.
-
Improves efficiency when handling long-running tasks.
-
Allows temporary suspension of applications.
-
Simplifies process management.
-
Reduces the need for multiple terminal windows.
-
Supports better resource utilization.
-
Enhances productivity for administrators and developers.
Conclusion
Job control is a powerful UNIX feature that allows users to manage multiple tasks efficiently from a single shell session. Through foreground and background execution, suspension, resumption, and job monitoring, users can maintain control over running processes without interrupting their workflow. Commands such as jobs, bg, fg, and kill form the foundation of job management, making UNIX a highly flexible and productive operating environment for multitasking.