FUNDAMENTALS A Complete Guide for Beginners
In Bash scripting, the ability to list and start multiple processes is invaluable for a variety of purposes including system administration, task automation, and parallel processing. In this guide, I’ll explore the fundamental concepts and some easy techniques to list and start multiple processes in Bash. Let’s delve into it!
What is Process in Bash?
A process in Bash is an executing instance of a program or command initiated by the shell. Each process is assigned a unique number called Process ID (PID) that provides a unique process identification and manipulation.
Following are the 2 basic types of processes in Bash:
A. Foreground Process
Foreground processes are the programs that execute in the current Bash shell allowing for user interaction. When a command is run in the foreground, it pauses the execution of other commands and holds control over the terminal until it terminates or is interrupted.
B. Background Process
Background processes are the programs initiated within the Bash shell that execute independently and allow users to continue working in the foreground. These processes allow other tasks to take over the execution operation.
How to List Processes in Bash?
To list processes in Bash, you can use ps command, top command, htop command or atop command. Some options can also be combined with these commands to sort processes by specific formats. In the following section, I’ll discuss the 4 different methods to list processes in Bash.
1. Using “ps” Command
The ps command stands for Process Status which produces a snapshot of all running processes and collects information about them. When using this command without any option or argument, it displays a list of running processes only for the current shell that includes four columns: PID, TTY, TIME, and CMD.
Following is an image showing the processes with the four crucial columns:
-
PID: Returns the unique Process ID.
-
TTY: Specifies the terminal type associated with the process.
-
TIME: Specifies the total amount of CPU time consumed by the process.
-
CMD: Returns the command name.
To get more in-depth information about all the running processes, use the following command:
ps aux
-
a: Displays information about all running processes of all users in the system.
-
u: Displays user-oriented outputs and provides additional information such as the owner of each process, CPU usage percentage, process starting time etc.
-
x: Lists all the processes that are not attached to the terminal such as daemon process (a process that runs in the background when the system is booted up).
Here are some of the other options that can be used with the ps
command to list the running processes:
-
ps -axjf: Lists processes in a hierarchical view.
-
ps -e or ps -A: Lists active Linux processes in the generic UNIX format.
-
ps -u [USERNAME]: Lists all running processes of a specified user.
-
ps -T: Displays active processes executed from the terminal.
2. Using “top” Command
The top command displays an updated list of processes in real time providing information about overall system resource usage. It is used to sort the list of processes in the order of decreasing CPU usage meaning that the process consuming the most resources will be appeared at the top of the list.
After running the top
command, you will see such an output:
However, some options can be used to change the output format based on the specified criteria while the top command is running. Some options are:
-
c: Displays the absolute path of the process.
-
h: Opens the help window.
-
M: Sorts the list by memory usage.
-
N: Sorts the list by PID.
-
q: Exits the command interface.
-
z: Changes the output color and highlights processes.
3. Using “htop” Command
The htop command displays the same information as the top command but in a more advanced and interactive way. It offers more features in a colorful graphical interface to easily navigate and list processes and allows users to scroll horizontally and vertically. Generally, the htop command is not installed by default in most Linux distributions. To install htop
command on any Debian/Ubuntu-based system, run the command below:
sudo apt install htop
Once installed, run the htop
command and see the list of all processes like the image below:
In this image, you can see all the processes in the system. Also, at the bottom of the output, there are a few short keys that serve different purposes.
4. Using “atop” Command
The atop
command offers a more comprehensive overview of all the running processes rather than the top command. To install it on a Debian/Ubuntu-based system, run the following command:
sudo apt install atop
Once completed, run the atop
command and you will see an output like the following:
Here are some of the short keys to sort processes in different standards while the atop command is running:
-
a: Sorts by most active resources.
-
c: Sorts by CPU consumption.
-
d: Sorts by disk activity.
-
m: Sorts by memory usage.
-
n: Sorts by network activity.
pstree -p
command.How to Start Multiple Processes in Parallel Using Bash Script?
Starting multiple processes in parallel reduces the total execution time of Bash programs. There are several straightforward methods to start multiple processes in Bash such as using the ampersand &
, xargs command and GNU parallel. In the next section, I’ll interpret these 3 methods to start multiple processes in parallel while executing Bash scripts.
1. Using Ampersand “&” with “wait” Command
The ampersand &
operator enables the concurrent execution of processes i.e. allows a Bash script to continue executing the next process without waiting for the current one to finish. To start multiple processes in the background, use the inherent ampersand &
operator with the wait command in Bash where the wait command waits for all the background processes (commands or tasks) to finish.
Basic Syntax:
command1&
command2&
wait
To start multiple processes using the ampersand &
with the wait
command, check out the following Bash script:
#!/bin/bash
#Defining commands
command1="ps u"
command2="hostname"
command3="date"
#Starting multiple processes in the background
$command1 &
sleep 2 #Sleep for 2 seconds
$command2 &
sleep 2 #Sleep for 2 seconds
$command3 &
#Waiting for all background processes to complete
wait
echo "Commands completed successfully"
Here, $command1 &
, $command2 &
and $command3 &
start the 3 commands in the background assigned to the variables using &. Then. the wait
command waits for all the commands in the background to complete. Finally, the script prints a process completion message.
As you can see in the image the script executed all the commands parallelly and displayed the outputs one by one.
2. Using “xargs” Command
The xargs command is used to read and process inputs and execute different commands based on the input arguments. It helps execute commands in parallel i.e. start multiple processes in Bash.
Go through the script below to start multiple processes using the xargs
command:
#!/bin/bash
#Defining an array of commands
multi_commands=(
"date"
"whoami"
"df -h"
)
#Executing commands in parallel using 'xargs' command
printf "%s\n" "${multi_commands[@]}" | xargs -I {} -P 3 sh -c "{}"
echo "Commands completed successfully"
In the script, the commands date, whoami and df -h display date, username and disk space usage data on mounted file systems respectively. The printf
command prints each command from the multi_commands array on a separate line. Then, the xargs
command reads the input lines from the printf command and executes each command in parallel with a maximum of 3 processes (-P 3
) using the option -I {}
that specifies the placeholder {} for each command. Here, each command is executed by sh -c
.
The image depicts that all three commands were executed in parallel.
3. Using GNU Parallel Tool
GNU parallel is a powerful shell tool for executing multiple processes (commands or tasks) in parallel. It enables faster execution providing flexibility in parallel processing. By default, GNU parallel is not included and needs to be installed separately. However, you can install this powerful utility on Ubuntu by running the following command:
sudo apt install parallel
Here’s a Bash script to start multiple processes using GNU parallel:
#! /bin/bash
multi_commands=(
"uptime"
"ncal"
"ls -l /home/nadiba/Downloads"
)
parallel --jobs 3 ::: "${multi_commands[@]}"
echo "Commands completed successfully"
Here, parallel --jobs 3
specifies a maximum number of 3 jobs (tasks) that run simultaneously. Here, :::
followed by "${multi_commands[@]}"
takes the arguments (input items) from the multi_commands array and passes them to parallel.
In the above images, all the commands were executed in parallel and displayed the outputs one after one.
Conclusion
In this article, I have mentioned the easy and effective methods to list and start multiple processes in Bash. By mastering the whole concept and all the methods, you can optimize your task execution and improve overall productivity.
People Also Ask
Can I start multiple processes in a Bash script?
Yes, you can start multiple processes in a Bash script using GNU parallel, xargs command and ampersand &
with wait command.
How do I get a list of processes in Linux?
To get a list of processes in Linux, use the following commands:
-
ps command: Lists all the running processes associated with the current shell session. Following are some common options that can be used with the ps command:
-
-
ps aux: Lists processes in the system in user-oriented format.
-
ps -ef: Lists detailed information about running processes.
-
-
pstree command: Displays running processes of a system in a hierarchical tree-like structure.
-
top command: Provides lists of running processes in real-time.
-
htop command: Provides an interactive view of running processes. (similar to ‘top’)
-
pgrep command: Lists processes based on specific criteria.
How to list a specific process?
To list a specific process, you can use the pgrep command with the particular process name using the syntax pgrep <PROCESS_NAME>
. This will list the PID associated with the process.
How do I see background processes in Linux?
To see background processes in Linux, you can use several commands such as ps command with different options, jobs command, pgrep command, top command and htop command.
Why List Processes in Bash?
Listing processes can serve various purposes such as:
- Process management
- Performance optimization
- System monitoring
- System automation
- Troubleshooting etc.
Is it possible to prioritize certain processes over others when starting multiple processes?
Yes, it is possible to prioritize certain processes over others when starting multiple processes. To accomplish the task, you can use the nice
command or renice
command and set the priority of a process. 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
- Check If a Process is Running in Bash [6 Methods]
- Process Management in Bash
- Exit Codes in Bash
- Bash Signals Handling
<< Go Back to Bash Process and Signal Handling | Bash Scripting Tutorial