Jobs and Job Control in Linux


Introduction

In the Linux operating system, jobs refer to processes that are running in the background or foreground. Job control refers to the ability to manipulate these processes, including suspending, resuming, and terminating them. This can be useful for managing multiple tasks or for debugging problems with a process.

Job control is made possible by the shell, which is a command-line interface that allows users to interact with the operating system. The most common shell in Linux is the Bourne Again Shell (BASH), but other shells such as the Z Shell (ZSH) and the Korn Shell (KSH) are also available.

In this article, we will explore the basics of job control in Linux and how to use it to manage processes.

Understanding Processes and Jobs in Linux

In Linux, every program that is running is considered a process. A process can be a standalone program or a part of a larger program.

Each process is assigned a unique identifier called a process ID (PID). The PID can be used to refer to the process and perform actions on it, such as suspending or terminating it.

A job is a process that is either running in the foreground or the background. The foreground is the active window in the terminal, and the background is any process that is running but not actively being used in the terminal.

By default, when you run a command in the terminal, it runs in the foreground. You can tell that a process is running in the foreground because it displays output and you cannot enter any more commands until it finishes.

To run a process in the background, you can use the & symbol at the end of the command.  

Example

$ sleep 30 &
[1] 12345

In this example, the sleep command causes the process to sleep for 30 seconds. The & symbol causes the process to run in the background, and the output [1] 12345 indicates that it is job number 1 with a PID of 12345.

Managing Jobs with the fg and bg Commands

The fg (foreground) and bg (background) commands allow you to move jobs between the foreground and the background.

To bring a background job to the foreground, you can use the fg command followed by the job number or the PID.  

Example

$ fg %1
sleep 30

This brings the sleep command, which is job number 1, to the foreground and displays its output.

To send a foreground job to the background, you can use the bg command followed by the job number or the PID.  

Example

$ sleep 30
[1] 12345
^Z
[1]+ Stopped      sleep 30
$ bg %1
[1]+ sleep 30 &

In this example, the sleep command is run in the foreground and then suspended with the ^Z keyboard shortcut. The bg command is then used to resume the job in the background.

Suspend or Resume Jobs in Linux

The suspend command allows you to temporarily stop a job, while the kill command allows you to permanently terminate a job.

To suspend a job, you can use the suspend command followed by the job number or the PID. 

Example

$ sleep 30 &
[1] 12345
$ suspend %1
[1]+ Suspended

This suspends the sleep command, which is job number 1. The job can then be resumed with the fg command or left suspended in the background.

To terminate a job, you can use the kill command followed by the job number or the PID.  

Example

$ sleep 30 &
[1] 12345
$ kill %1

This terminates the sleep command, which is job number 1.

View and Manage Jobs with the jobs and ps Commands

The jobs command allows you to view a list of all jobs running in the background or suspended in the current shell. The ps command allows you to view a list of all processes running on the system.

Example

$ sleep 30 &
[1] 12345
$ sleep 60 &
[2] 12346
$ jobs
[1] Running    sleep 30 &
[2] Running    sleep 60 &

You can use the jobs command to view the status of each job and its job number or PID.

The ps command allows you to view a list of all the processes that are currently running on the system. You can use the -a flag to show all processes and the -x flag to show processes that are not associated with a terminal.

Example

$ ps -ax
   PID TTY   STAT    TIME COMMAND
      1 ?    Ss      0:00 /sbin/init
      2 ?    S       0:00 [kthreadd]
      3 ?    S       0:00 [ksoftirqd/0]
      4 ?    S       0:00 [kworker/0:0]
...

The ps command displays the PID, terminal (TTY), status, time, and command for each process.

Conclusion

In Linux, job control allows you to manage the processes that are running on your system. You can use commands such as fg, bg, suspend, and kill to manipulate jobs and the jobs and ps commands to view and manage them. Understanding and using these commands can help you effectively manage your system and troubleshoot problems.

Updated on: 05-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements