Unix - UNIX Job Control
Unix job control is a feature that allows users to manage multiple processes within a terminal session. It provides the ability to start programs, pause them, move them between the foreground and background, and monitor their status. This capability is especially useful when working in command-line environments because it allows efficient multitasking without opening multiple terminal windows.
In Unix, every command you run in the shell becomes a process. The shell tracks these processes and organizes them into jobs. A job can consist of a single process or multiple processes linked together, such as in a pipeline. Job control gives the user the ability to control these jobs interactively during a shell session.
Foreground and Background Jobs
A foreground job is a process that takes control of the terminal. When a command runs in the foreground, the shell waits until the process completes before accepting another command. For example, when you run a text editor or a script normally, it occupies the terminal until it finishes.
A background job runs without taking control of the terminal. This means the user can continue entering commands while the process executes. Background execution is useful for long-running tasks such as backups, file transfers, or software compilation.
To run a process in the background, the ampersand symbol & is used at the end of a command.
Example:
sleep 100 &
This command starts the process in the background, and the shell immediately returns the prompt.
Process Suspension
Unix allows temporarily stopping a running foreground process without terminating it. This is known as suspending the process. Suspension is commonly done by pressing Ctrl + Z.
When a process is suspended, it remains in memory but stops executing. The shell marks it as stopped and assigns it a job number.
Example:
cat largefile.txt
While this command is running, pressing Ctrl + Z suspends it. The shell may display:
[1]+ Stopped cat largefile.txt
This indicates that job number 1 is currently paused.
The jobs Command
The jobs command displays all active jobs in the current shell session. It shows whether jobs are running, stopped, or completed.
Example:
jobs
Output:
[1]+ Stopped cat largefile.txt
[2]- Running sleep 100 &
The symbols + and - indicate the default jobs used by certain commands like fg and bg.
The jobs list is local to the current shell session. If you open a new terminal window, it will not show the same jobs.
Moving Jobs to Background with bg
The bg command resumes a suspended job and runs it in the background.
Example:
bg %1
This resumes job 1 in the background.
The %1 refers to job number 1. After this, the process continues running while the shell becomes available for new commands.
This is useful when you accidentally start a long task in the foreground and want to continue using the terminal.
Bringing Jobs to Foreground with fg
The fg command brings a background or suspended job back to the foreground.
Example:
fg %2
This moves job 2 to the foreground, giving it terminal control.
If no job number is specified, fg brings the most recent job to the foreground.
This is commonly used when you need to interact with a process that was previously sent to the background.
Job Numbers and Process IDs
Unix distinguishes between job numbers and process IDs.
Job number:
-
Assigned by the shell
-
Temporary
-
Used only in the current shell session
Process ID (PID):
-
Assigned by the kernel
-
Unique system-wide
-
Used by system utilities like
kill
Example:
ps
This command shows PIDs.
A job might have job number 2 in the shell but PID 4352 in the system. These are different identifiers.
nohup Command
The nohup command allows a process to continue running even after the user logs out. Normally, when the terminal session ends, background processes may terminate. nohup prevents this by ignoring hangup signals.
Example:
nohup ./backup.sh &
This starts the script in the background and ensures it keeps running after logout.
Output is usually redirected to a file named nohup.out.
This command is important for long-running jobs on remote servers.
Job Control Signals
Job control relies heavily on signals. Signals are software interrupts sent to processes.
Common signals include:
-
SIGSTOP— Stops a process -
SIGCONT— Continues a stopped process -
SIGINT— Interrupts a process -
SIGHUP— Sent when terminal closes
These signals help the shell manage jobs.
For example, pressing Ctrl + Z sends a stop-related signal to suspend the foreground job.
Practical Example
Suppose a user starts a command:
find / -name "*.log"
This command may take a long time.
The user presses:
Ctrl + Z
The process is suspended.
Then:
bg
The process continues in the background.
To check status:
jobs
To return it to the foreground:
fg
This sequence demonstrates typical job control operations.
Importance of Job Control
Unix job control is important because it improves command-line productivity. Users can manage multiple tasks from one terminal efficiently. It avoids unnecessary termination of processes and provides flexibility in handling long-running tasks.
System administrators frequently use job control for server management, monitoring logs, running scripts, and handling maintenance tasks. Developers use it during compilation, testing, and debugging.
Without job control, users would need separate terminal sessions for each task, reducing efficiency.
Limitations
Job control works only in interactive shells. Scripts executed non-interactively do not support direct job control commands.
Some shells support advanced job control, while others have limited features.
If the terminal session crashes, jobs without nohup may terminate.
Job control is mainly a shell-level feature and does not replace full process management tools.
Summary
Unix job control is the mechanism that lets users manage processes interactively in the shell. It includes starting commands in the background, suspending them, resuming them, and switching them to the foreground.
Main commands used are:
-
jobsfor listing jobs -
bgfor background execution -
fgfor foreground execution -
nohupfor persistent execution after logout
This feature is fundamental for multitasking in Unix environments and plays a major role in system administration and command-line efficiency.