Managing Processes
Last updated
Last updated
In Linux, a process goes through several stages during its lifetime. Understanding these stages and checking how they run in the background is important for process management and troubleshooting. The states of a process in Linux are as follows:
Created: A process is created when a program is executed. At this stage, the process is in a "created" state, and its data structures are initialized.
Ready: The process enters the "ready" state when it is waiting to be assigned to a processor by the Linux scheduler. At this stage, the process is waiting for its turn to execute.
Running: The process enters the "running" state when it is assigned to a processor and is actively executing its instructions.
Waiting: The process enters the "waiting" state when it is waiting for some event to occur, such as input/output completion, a signal, or a timer. At this stage, the process is not actively executing its instructions.
Terminated: The process enters the "terminated" state when it has completed its execution or has been terminated by a signal. At this stage, the process data structures are removed, and its resources are freed.
Zombie: A process enters the "zombie" state when it has completed its execution but its parent process has not yet read its exit status. At this stage, the process details still have an entry in the process table, but it does not execute any instructions. The zombie process is removed from the process table when its parent process reads its exit status.
Processes are classified into 2 types in Linux Distributions:
Foreground Processes (FG)
Background Processes (Non-Interactive Processes):
There are several commands used in process management in Linux. Here are some of the most commonly used commands:
ps: This command is used to display information about running processes. The "ps" command can be used to list all processes or filter the list based on various criteria, such as the user who started the process, the process ID (PID), and the process status.
top: This command is used to display a real-time view of system processes. The "top" command provides information about the processes running on the system, including their resource usages, such as CPU and memory.
kill: This command is used to terminate a process. The "kill" command can be used with the process ID (PID) of the process or with a signal number to request a specific action.
nice: This command is used to adjust the priority of a process. Higher-priority processes get more CPU time than lower-priority processes. The "nice" command can be used to increase or decrease the priority of a process, which affects its CPU usage.
renice: This command is used to change or adjust the priority of a running process, which affects its CPU usage.
pkill: This command is used to send a signal to a process to request it to terminate. The "pkill" command can be used with a current process name or a regular expression to match multiple processes.
top: This command is used to display a real-time view of system processes. The "top" command provides information about the processes running on the system, including their resource usages, such as CPU and memory.
jobs: This command is used to display a list of background jobs running in the current shell session.
fg: This command is used to move a background process to the foreground. The "fg" command can be used with the job ID of the background process.
bg: This command is used to move a suspended process to the background. The "bg" command can be used with the job ID of the suspended process.
The built-in jobs utility lists the jobs that are running in the current terminal session, while fg
returns a job to the foreground. These commands are shown in action below:
FG
In Unix-like operating systems, backgrounding a process refers to running a process in the background, allowing the user to continue using the terminal for other tasks while the background process continues to run. This can be particularly useful for long-running tasks. Here’s a quick guide on how to manage background processes using the bg
command and other related commands.
When you start a process, you can run it in the background by appending an ampersand (&
) to the command. For example:
If you have already started a process in the foreground and want to move it to the background, you can do so using the following steps:
Suspend the process: Press Ctrl+Z
to stop (pause) the running process and put it into the background in a suspended state.
Background the process: Use the bg
command to resume the process in the background.
To see a list of all background jobs, use the jobs
command:
This will show a list of jobs along with their job IDs, states, and the commands that started them.
If you need to bring a background job back to the foreground, use the fg
command followed by the job ID. For example:
This command will bring job number 1 to the foreground.
If you want to send a currently running process to the background without suspending it first, you can start the process in the background using the ampersand (&
) as shown above. However, for processes that are already running in the foreground, you must first suspend them using Ctrl+Z
, and then background them using bg
.
Here is an example workflow that demonstrates these commands:
Start a process in the foreground:
Suspend the process:
Output will be similar to:
Move the suspended process to the background:
Output will be:
Check the background jobs:
Output will be similar to:
Bring the background process to the foreground:
This brings job number 1 back to the foreground.
Start a process in the background: command &
Suspend a running process: Ctrl+Z
Resume a suspended process in the background: bg
List background jobs: jobs
Bring a background job to the foreground: fg %job_id
Understanding how to manage background processes can significantly improve your productivity when working with the command line in Unix-like operating systems.