How to List and Start Multiple Bash Processes? [Easy Ways]

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:

List processes in Bash using 'ps' command

OUTPUT ANALYSIS
  • 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

EXPLANATION
  • 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).

List processes in Bash using 'ps aux' command

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:

List processes in Bash using 'top' command

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:

List processes in Bash using 'htop' command

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:

List processes in Bash using 'atop' command

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.

Note: To list processes with their PIDs in a tree-like structure, use the 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"

EXPLANATION

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.

Start 3 processes in parallel in Bash using ampersand &

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"

EXPLANATION

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.

Start 3 processes in parallel in Bash using "xargs" command

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"

EXPLANATION

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.

Start 3 processes in Bash using GNU 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:

  1. 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.

  1. pstree command: Displays running processes of a system in a hierarchical tree-like structure.

  2. top command: Provides lists of running processes in real-time.

  3. htop command: Provides an interactive view of running processes. (similar to ‘top’)

  4. 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:

  1. Process management
  2. Performance optimization
  3. System monitoring
  4. System automation
  5. 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


<< Go Back to Bash Process and Signal Handling | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment