Check If a Process is Running in Bash [6 Methods]

The term ‘running process’ describes a program that is currently running in the system’s memory. Whether you are a beginner or a proficient user or developer, there are times when you need to know if a particular process is active on the system. In this effort, Bash offers several ways to check if a process is running.

In this article, I will demonstrate 6 methods to check if a process is running in Bash.

What is a Process?

A process is a task or executing instance of a command or program that a machine is currently working on. Every time a command is executed, the system recognizes it as a process and saves it in memory. In Linux, every process running in the system is assigned a unique number called Process ID (PID) which is a numeric value and provides a unique identification to each process.

6 Methods to Check If a Process is Running in Bash

Most of the time, a system runs a lot of processes. Some of these processes run for a long time, while others run quickly. To check out all these running processes, there are several methods such as using ps command, pgrep command, pidof command, proc file system, top command, and htop command.

In the following section, you will see how to set up a simple process and check if it is running in Bash:

Setting up a Process

First, let’s create a script like the following that takes a long time to finish:

#!/bin/bash

echo "Initializing..."

#A time-consuming operation with a loop
for ((i = 0; i < 200; i++)); do
    echo "Simulating.. Iteration $i"
    sleep 50  #For each iteration, sleep for 50 seconds
done

echo "Done"

Now, run the script and start the process in the background like the following image:

Executing a time-consumed script in the background of a system

The ampersand & at the end allows a user to continue using the Bash shell while the script ‘process.sh’ executes asynchronously in the background of the system. Generally, this is used in the case of long-running processes.

Go through the following 6 methods to check out the status of the running process:

1. Using “ps” Command

The ps command stands for Process Status which is used to monitor the currently running processes and get information about them. It has various options to list all the processes and monitor their status.

To check if a specific process is running in the system, pipe the grep command with the ps command like the following:

ps -ef | grep process.sh
EXPLANATION
  • ps: Displays the status of the currently running process on a system.
  • -ef: Specifies two combined options where -e selects all processes running in the system and -f provides a full listing format of the processes.
  • |: The pipe operator allows the output of ps -ef to be used as the input of grep process.sh.
  • grep process.sh: Searches for lines containing the string ‘process.sh’ in the output from the ps -ef.

Using the "ps" command piped with the "grep" command to check if a process is running in Bash

From the first line of the command output, you can see the script name with the PID 5757 and the parent PID 5518 which means the script is running.

The second line listed in the output is just showing the output of the grep command piped with the ps command.

2. Using “pgrep” Command

The pgrep command in Bash searches for the patterns among the currently running processes by their names and attributes and prints the corresponding PIDs according to the options. Using this command is convenient only for those cases when you only need to check if a process is active, rather than the details about the process.

Here’s how to check if a process is running using the pgrep -l command:

pgrep -l process.sh
EXPLANATION
  • pgrep -l: The option “-l” with pgrep command lists PID and process name.
  • process.sh: Represents the process name or pattern you are searching for.

Using "pgrep -l" command to check if a process is running

As you can see the -l option lists PID (3515) and process name (process.sh) in the image. So, it’s confirmed that the process is running.

To track active processes, you may use more options with the pgrep command by navigating to the pgrep --help page.

3. Using “pidof” Command

The pidof command in Bash is used to find and print the PID of a process. It’s one of the straightforward ways to obtain the PID of a running program.

To check if a process is running in Bash, use the pidof -x command in the following manner:

pidof -x process.sh
EXPLANATION
  • pidof -x: Searches for the associated PID of the running process based on its executable file name.

Using "pidof -x" command to check if a process is running

The image shows that the -x option specifies the PID 3697 of the running program.

4. Using the “proc” File System

The /proc file system is a virtual file system in Linux that provides information about kernel parameters. This /proc file system only exists in the memory while the system is running. Each directory in /proc contains a numeric name i.e. PID and represents a running process. The status files inside the directories contain the basic information about the particular processes.

To check if a process is running in the system, grep through the status file in the /proc file system like the following:

grep "process.sh" /proc/[0-9]*/status | awk -F: '{split($1,a,/\//); print a[3] $3}'
EXPLANATION
  • grep “process.sh” /proc/[0-9]*/status: The grep command searches for the string ‘process.sh’ in the status files of processes listed in the /proc directory.
  • |: The pipe operator allows the output of the grep command to be used as the input of the awk.
  • awk -F:: The -F: option with the awk command specifies the field separator as a colon (:).
  • ‘{split($1,a,/\//);: Using the piped output, the awk command splits the first field, $1 (the path to the process directory), into the array a where / acts as the delimiter.
  • print a[3] $3}’: Prints the PID extracted from the split operation (a[3]) followed by the relevant information from the third field, $3.

Using "/proc" file system to check if a process is running

In the above image, you can see the PID 11832 and the process name process.sh which indicates the process is successfully running in my system.

5. Using the “top” Command

The top command in Linux displays an updated list of all running processes in the system including different system metrics such as PID, execution time, CPU usage, memory usage, etc. You can customize the display by pressing different keys based on your needs.

To see the list of running processes sorted by user, run the top command in the terminal, then press SHIFT + U, write the specific user name, and click ENTER.

Setting specific user in the list of processes in "top" command

Now, scroll down the list to find the particular process you are looking for.

Using "top" command to check if a process is running

From the image, you can see that the process I was searching for exists in the list. It means the process is running in my system.

6. Using the “htop” Command

The htop command in Linux provides a more interactive interface while showing all the running processes in the system. It is the advanced version of the traditional top command.

To search for a specific process, run the htop command in the terminal, press F3, write the process name and press ENTER.

Using "htop" command to check if a process is running

In the above image, you can see that the process name is present in the interactive list meaning that it is running in my system.

If the htop command is not installed on your system, install it using sudo apt install htop syntax [Debian/Ubuntu-based system].

Conclusion

So far, you have learned various ways to check if a process is running in Bash. Among them, some commands like pgrep and pidof provide very little information regarding the process whereas the ps command displays detailed information. It’s up to you to decide which one to go with.

People Also Ask

What is the purpose of checking if a process is running in Bash?

Checking if a process is running in Bash serves various purposes like system monitoring, error handling, resource management, system reliability, automation, etc.

What is process ID in Linux?

In Linux, a Process ID (PID) is a numerical identifier assigned to every process running on a system. The PID cannot be the same for two processes at once. Every process contains a unique PID of its own.

How long does a PID stay assigned to a process in Linux?

The time duration of a PID in Linux depends on the execution of the corresponding process. It stays assigned until the process execution is completed. Once the process is completed or terminated, the PID is released and ready to be used by another process.

Is there a one-liner to check if a process is running?

Yes, You can use the pgrep command as a one-liner to check if a process is running. For example:

pgrep <process_name> && echo "The process is running." || echo "The process is not running."

Can I check if multiple instances of a process are running?

Yes, you can check for multiple instances of a running process using the pgrep command. For example:

pgrep -c <process_name> 

Where c represents the count of matching processes.

Related Articles


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

3/5 - (2 votes)
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