Unix - UNIX Job Control (Foreground and Background Processes)

UNIX is a multitasking operating system that allows users to run multiple processes simultaneously. Job control is a feature provided by the UNIX shell that enables users to manage multiple processes from a single terminal session. It allows users to start, stop, suspend, resume, and move processes between the foreground and background.

Understanding job control is important for system administrators, developers, and users who frequently work with command-line applications.

What is a Job?

A job is a process or a group of processes started from a shell. Every command executed in the terminal creates a process. The shell assigns a job number to each process it manages.

For example:

sleep 100

This command starts a process that remains active for 100 seconds.

The shell tracks this process as a job and allows the user to control it.

Foreground Processes

A foreground process is a process that occupies the terminal and interacts directly with the user. When a process runs in the foreground:

  • It receives input from the keyboard.

  • It displays output directly on the terminal.

  • The shell waits until the process finishes before accepting another command.

Example:

cat

The cat command waits for user input and echoes it back to the terminal. Since it requires interaction, it runs in the foreground.

Characteristics of foreground processes:

  • Interactive in nature.

  • Have direct access to terminal input and output.

  • Prevent execution of new commands until completion.

Background Processes

A background process runs independently of the terminal and does not require immediate user interaction. The shell allows users to continue working while the process executes.

To start a background process, use the ampersand (&) symbol.

Example:

sleep 100 &

Output:

[1] 12345

Where:

  • 1 is the job number.

  • 12345 is the process ID (PID).

The shell immediately returns the prompt, allowing the user to execute additional commands.

Benefits of background processing:

  • Improves productivity.

  • Allows execution of long-running tasks without blocking the terminal.

  • Supports multitasking.

Viewing Active Jobs

The jobs command displays all jobs running under the current shell session.

Example:

jobs

Output:

[1]+ Running sleep 100 &
[2]- Running ping google.com &

Information displayed includes:

  • Job number.

  • Status of the job.

  • Command associated with the job.

Common statuses:

Status Meaning
Running Process is executing
Stopped Process is suspended
Done Process completed successfully
Terminated Process ended abnormally

Suspending a Foreground Process

Sometimes a process is running in the foreground, but the user wants to temporarily stop it.

This can be done using:

Ctrl + Z

Example:

find / -name "*.log"

Pressing Ctrl + Z produces:

[1]+ Stopped find / -name "*.log"

The process is not terminated; it is merely paused.

Advantages:

  • Allows temporary interruption.

  • Preserves process state.

  • Enables later continuation.

Moving a Process to the Background

A suspended process can be resumed in the background using the bg command.

Example:

bg %1

Output:

[1]+ find / -name "*.log" &

The process continues running without occupying the terminal.

The %1 notation refers to job number 1.

If only one job exists:

bg

is sufficient.

Bringing a Background Process to the Foreground

A background process can be brought back to the foreground using the fg command.

Example:

fg %1

The selected job resumes in the foreground and regains terminal control.

This feature is useful when:

  • User interaction becomes necessary.

  • Monitoring process output is required.

  • Process completion needs supervision.

Terminating Jobs

Jobs can be terminated using the kill command.

First, identify the process:

jobs

Then terminate it:

kill %1

or

kill 12345

where 12345 is the process ID.

To force termination:

kill -9 12345

The operating system immediately stops the process.

Difference Between Jobs and Processes

Although often used interchangeably, jobs and processes are different.

Job Process
Managed by the shell Managed by the operating system
Identified by job number Identified by PID
Exists only in current shell Exists system-wide
Used for job control Used for process management

A single job may contain multiple processes.

Process Groups

UNIX organizes related processes into process groups.

For example:

ls | grep txt | sort

This pipeline contains three separate processes:

  • ls

  • grep

  • sort

The shell treats them as one job and assigns a single job number.

Benefits include:

  • Easier management.

  • Unified suspension and resumption.

  • Simplified signal handling.

Signals Used in Job Control

Signals are messages sent to processes to control their behavior.

Important job-control signals include:

SIGINT

Generated by:

Ctrl + C

Terminates the foreground process.

SIGTSTP

Generated by:

Ctrl + Z

Suspends the process.

SIGCONT

Used to resume a suspended process.

Generated internally by:

bg

or

fg

SIGTERM

Requests graceful termination.

kill PID

SIGKILL

Forces immediate termination.

kill -9 PID

Running Long Tasks in the Background

Consider a large backup operation:

tar -czf backup.tar.gz /home &

The backup runs in the background while the user continues other work.

Monitoring can be done using:

jobs

or

ps

This approach is commonly used for:

  • Backups

  • Log analysis

  • Data processing

  • Software compilation

  • File transfers

Using nohup for Persistent Background Processes

Normally, background processes terminate when the user logs out.

To keep a process running after logout:

nohup command &

Example:

nohup python app.py &

Output is stored in:

nohup.out

This is useful for:

  • Servers

  • Daemons

  • Long-running scripts

  • Batch processing jobs

Practical Example

Step 1: Start a process.

sleep 300

Step 2: Suspend it.

Ctrl + Z

Step 3: View jobs.

jobs

Output:

[1]+ Stopped sleep 300

Step 4: Resume in background.

bg %1

Step 5: Check jobs again.

jobs

Output:

[1]+ Running sleep 300 &

Step 6: Bring it back to foreground.

fg %1

The process returns to terminal control.

Best Practices for Job Control

  1. Use foreground execution for interactive programs.

  2. Run long-running tasks in the background whenever possible.

  3. Monitor active jobs regularly using the jobs command.

  4. Use nohup for processes that must survive logout.

  5. Avoid forcefully killing processes unless necessary.

  6. Understand signal behavior before terminating critical applications.

  7. Organize related tasks using process groups.

Conclusion

Job control is a powerful UNIX feature that enables efficient management of multiple processes within a single terminal session. By understanding foreground and background execution, job suspension, resumption, process groups, and signal handling, users can work more productively and manage system resources effectively. Mastering job control is essential for anyone who regularly uses the UNIX command line, as it forms the foundation of multitasking and process management in UNIX environments.