Process management is a fundamental aspect of Bash that controls and monitors the processes running on a system. It includes managing process resources, and prioritizing and terminating processes when required.
In this article, I’ll walk you through the basics of processes as well as a comprehensive guide on how to list, start, monitor, control, terminate and prioritize processes in Bash. Let’s explore!
What is Bash Process?
A process is a task or executing instance of a program currently running on a system. Whenever an application, a script, or a command is executed, Bash interprets it as a process. Each process is assigned a unique identification number called Process ID (PID) that makes it distinct from other processes on the system.
Types of Processes in Bash
In Linux, processes can be of two types. They are:
A. Foreground Processes
Foreground processes are the kind of programs that execute in the current Bash shell and require user interaction. A foreground process usually takes control of the terminal by preventing other commands from running until the foreground process terminates or is interrupted.
B. Background Processes
Background processes are non-interactive programs that execute independently in the background. It allows the user to continue their work in the foreground while allowing other tasks to run in the background.
Additionally, some common types of processes in Bash are:
-
Parent process: Specifies the process that creates other processes. In the process tree, the init process is the first process that acts as the parent process of other processes. Generally, a parent process can have multiple child processes and can wait for the child processes to finish their execution or they can continue their execution independently. Every parent process in a system includes a unique identification number called Parent Process ID (PPID).
-
Child process: Specifies the process created by another process (parent process). All child processes inherit certain features from their parent processes.
-
Zombie process: Specifies the process that has already completed its execution but still shows up in the process table.
-
Orphan process: Specifies the child process when its parent process gets executed before it. In this case, the orphan processes are typically adopted by the init process (the new PPID), which ensures their continuous execution and prevents them from becoming zombie processes.
-
Daemon process: Specifies the system-related process that runs continuously in the background.
-
Interactive process: Specifies the process that directly interacts with the user via the terminal.
Different States of Bash Process
A process in Linux can go through different states. They are as follows:
-
Ready State: The process waits for its turn to be executed.
-
Running State: The process is actively executing on the system.
-
Sleeping State: The process waits for a resource to be available.
-
-
Interruptible Sleep State: The process wakes up and responds to signals.
-
Uninterruptible Sleep State: The process does not respond to signals.
-
-
Stopped State: The process is suspended by a signal.
-
Terminated State: The process completes its execution or has been terminated by a signal.
-
Zombie State: The process is dead but still has an entry in the process table.
Commands for Process Management in Linux
There are several commands in Bash for process management. Following are some commonly used commands:
Command |
Purpose |
---|---|
ps |
Lists all processes in the system. |
top |
Tracks the running processes on the system and provides real-time information. |
kill |
Terminates a running process by sending a signal. |
pkill |
Sends a termination signal to a process. |
jobs |
Lists all the running jobs (tasks) in the system. |
fg |
Moves a background process to the foreground. |
bg |
Restarts a stopped process to run in the background. |
nice |
Adjusts the priority of a process. |
renice |
Changes or adjusts the priority of a running process. |
free |
Displays the status of RAM. |
df |
Shows the disk management of the system. |
Process Management Approaches in Bash
In this section, I’ll explain how to list, launch, and terminate processes using different commands in Bash.
Listing Processes in Bash
To list and view detailed information about all the Bash processes running on the system, use the following command:
ps aux
Some options can be used with the ps
command to filter the process list based on specific criteria such as
Sort by user:
ps -u USERNAME
Sort by PID:
ps -p PID
Sort by command name:
ps -C COMMAND_NAME
How to List Bash Processes in Hierarchy?
To list Bash processes with their PIDs in a hierarchical format, you can use the command below:
pstree -p
You can also use some command line utilities such as top command, htop command, atop command to list and keep track of process resource usage.
Launching Processes in Bash
-
Running a Foreground Process: To launch a process in the foreground, simply run the command or script in the terminal. For example:
./my_script.sh
-
Running a Background Process: To run a process in the background, use an ampersand
&
at the end of the command or script. For example:
./my_script.sh &
-
Running Multiple Processes: To start multiple processes in parallel, you can run each process in the background by appending an ampersand
&
at the end of each command. Here’s how you can do it:
command1 & command2 & command3 &
Following is an example showing the concurrent execution of multiple processes:
How to Kill/Terminate Process in Bash?
The kill command is used to terminate processes running on a system. It sends a signal to the particular process. By using the command kill -L
, you can list all the signals that you can use.
By default, the kill command when used without specifying a number sends the SIGTERM (15) signal to a process for termination.
The basic syntax to kill a process is:
kill PID
To forcefully terminate a process, use the following command
kill -9 PID
This command sends a SIGKILL (9) signal to the process and it is used when a process does not respond to the regular termination signal.
kill -SIGKILL PID
or kill -KILL PID
Managing Priority of Processes in Bash
Managing a process’s priority involves altering the order of priority in which processes are executed by the system. By setting the priority, you can make sure that the most important tasks get the most CPU time to receive the necessary system resources compared to the less important tasks. Some common aspects of prioritizing processes:
1. Process Priority Range
The priority value of a process is known as the ‘nice value’ that typically ranges from -20 to 19, where -20 indicates the highest priority and 19 indicates the lowest priority. By default, the processes launched from the terminal have a zero (0) value of priority. The negative values (-20 to 0) are reserved for real-time tasks while the positive values (0-19) are for normal user processes or tasks.
2. Using “nice” Command
The nice command starts a process with a specified priority level. It assigns a lower priority to a process as the lower the nice values, the higher the priority. The syntax to use the nice command is nice -n <PRIORITY> <COMMAND>
.
3. Using “renice” Command
The renice command modifies and adjusts the priority of a process that is already running. This command either increases or decreases the priority of a process based on the specific value. The syntax to use the renice command is renice <PRIORITY> -p <PID>
.
Decreasing priority can be done by any user in the system but the priority can only be increased by the process owner or the super user (root).
Conclusion
By incorporating all the facts and techniques outlined in this guide, you can easily navigate the complexities of process management, efficiently handle tasks, and optimize system resources
People Also Ask
How do I terminate a process in Linux?
To terminate a process in Linux, use the kill
command with the specific PID of the process. For example:
kill [PID]
How can I run a process in the background in Linux?
To run a process in the background in Linux, use an ampersand &
at the end of the command. For example:
./script.sh &
How do I find out which processes are consuming the most resources?
To find out which processes are consuming the most resources, you can check out the following options:
-
Use
ps aux
command to list all processes with detailed information like CPU and memory usage. -
Run
top
command and then press SHIFT + M to sort processes by memory usage and press SHIFT + P to sort processes by CPU usage. -
Run
htop
command and press F6 to sort processes by specific criteria like CPU or memory usage.
Can I prioritize certain processes over others in Linux?
Yes, you can prioritize certain processes over others in Linux using the nice
and renice
commands. For example:
#!/bin/bash
#Starting command1 with a higher priority
nice -n -10 command1 &
#Starting command2 with default priority (priority value of 0)
command2 &
#Starting command3 with a lower priority
nice -n 10 command3 &
Related Articles
- How to Add Delays in Bash? [Sleep for a Time]
- Bash “wait” Command in Process Management [6 Examples]
- How to Measure Bash Elapsed Time? [Seconds to Milliseconds]
- How to Shutdown Linux? [Fixed “Shutdown Command Not Found”]
<< Go Back to Bash Process and Signal Handling | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners