1. Overview

We usually have a lot of processes running on our machine. Some of them run for a very long time, while others only run briefly. And occasionally, we’ll need to monitor and control them. There are many commands available in the Linux ecosystem for managing them.

In this tutorial, we’ll see different approaches for checking if a process is running or not.

2. Setup

First, we’ll need a script that takes a long time to finish:

$ cat run.sh 
#!/bin/bash
echo 'Starting...'
sleep 1000
echo 'Completed'

Then, we’ll start it in the background:

$ ./run.sh & 
[1] 17838
Starting...

Next, let’s check the status of the script using different commands.

3. Using the ps Command

The ps command is one of the most commonly used Linux commands to monitor the process status.

It comes with various options to list the processes and their parameters. Consequently, this command provides us with detailed information regarding the processes:

$ ps -ef | grep run.sh
bluelake 17838  2411  0 14:47 pts/0    00:00:00 /bin/bash ./run.sh
bluelake 17842  2411  0 14:47 pts/0    00:00:00 grep --color=auto run.sh

Firstly, we’ve run the ps command and piped the result to the grep command. Secondly, in the grep command, we searched for the script name.

Let’s look at the different options we’ve used for the ps command:

  • -e: select all processes running in the system
  • -f: to view the full format listing of the process

Finally, we could see the script name in the result, which confirms that the script is indeed running. Typically, the ps command shows the PID (17838) and the parent PID (2411), as we can see here.

We could see one more process listed in the results. This is the grep command in the pipeline. By looking at it, we shouldn’t assume that the process is running as this will be always shown in the result.

4. Using the pgrep Command

Sometimes, we only need to check whether a process is active. We may not be interested in minute details regarding the process. For those situations, we can use the pgrep command.

Here’s the syntax of pgrep:

pgrep [options] pattern

The pgrep command searches for the pattern among the currently running processes. And, once it finds a process it prints the PID and the process name depending upon the options we give while running the command.

Let’s see how we can use pgrep:

$ pgrep -l run.sh
18259 run.sh

Here, we can see it listed the running PID and its name. We’ve used the -l option to list the name of the process.

We may use other options as well:

  • -c: to get the count of processes
  • -f: to search in the full command including the arguments list
  • -a: to print the command line along with the PID
  • -i: to ignore case while searching
  • -x: list the process which exactly matches the pattern
  • -d: to set a different delimiter if we have more than one process (by default, a newline is used as a delimiter)

5. Using the pidof Command

The pidof command finds and prints the PID of a process. It’s similar to the pgrep command we saw above.

Let’s see the syntax of pidof: 

pidof [-s] [-c] [-q] [-w] [-x] [-o omitpid[,omitpid...]...]  [-S separator] program [program...]

Now, let’s run it:

$ pidof -x run.sh
18462

We’ve run the pidof command with the -x option to include any running scripts in the search. And it has listed the PID of the script we started.

In this command, we can give more than one process name or script name to search for. It prints the PIDs in a single line. As a result, we can then pipe these resulting PIDs into another command to perform additional operations on the PIDs found.

6. Using the proc File System

The proc file system is another place where we can find the information related to different processes in our system. We can find it in most of the Linux distributions out there. Therefore, this method of finding information related to the process is intended to work in the majority of situations.

The proc file system stores the kernel information. It stores the live system data organized in different files and folders. We can look into those files for specific information.

Apart from other files, this file system has a directory named after the PID for each process. Inside that directory, we can find a lot of files containing the metadata related to that process. Out of those, we can find a status file that contains basic process information in a human-readable format.

The idea here is to grep through this file in all these PID subdirectories:

$ grep "run.sh" /proc/[0-9]*/status | awk -F: '{split($1,a,/\//); print a[3] $3}'
19071	run.sh

Here, we’ve used the grep command to search in the PID subdirectories for the process name. Then, the results, which contain the file path and process name, are piped to the awk command. From the file path, we extract the PID using the split function. Finally, we print the PID and process name.

7. Conclusion

In this article, we saw different ways to check if a process is running.

We’ve seen that the pgrep and the pidof commands show minimal information about the processes.

On the other hand, the ps command displays more information. Furthermore, if we need a deeper understanding we can use the proc file system.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.