1. Overview

Sometimes, we want to find a specific process running on our system. The most common solution for this is to use a combination of ps and grep.

In this short tutorial, we’ll look at how to narrow a search for a running process. We’ll see how to solve a common problem where the grep statements used also appear in their own search results.

2. Finding Processes

2.1. Listing All Processes

Let’s start by listing the processes running on our machine. First, let’s open a new terminal and start a vi session:

% vi 

Now, using a second terminal, let’s use the ps command to see the current list of processes:

% ps

The output is:

  PID TTY           TIME CMD
 2584 ttys000    0:00.07 -zsh
 2589 ttys001    0:00.03 -zsh
 2592 ttys001    0:00.07 vi

Depending on the number of processes, the ps output might be too long to be useful. We often need to narrow down our search.

2.2. Looking for a Specific Process

To look for a specific process or processes, we can add the grep command to narrow down our results. Continuing our example, to find the vi instances running on our machine, let’s run:

% ps | grep vi

which gives us:

 3886 ttys000    0:00.00 grep vi
 2592 ttys001    0:00.07 vi

The output of our command includes our vi session from above. However, it also includes the grep process we used as a filter, which isn’t as helpful.

Let’s now take a look at a couple of ways to remove the grep from our results.

3. Excluding grep

3.1. Removing grep With grep

One way to exclude the grep line from ps output is to use an additional grep with the -v option to invert the search:

% ps | grep vi | grep -v grep

This outputs:

 2592 ttys001    0:00.07 vi

In this case, the -v option allows everything from our grep vi results except lines containing the string “grep”. This works, but it requires an additional process and is longer to type.

3.2. Make a grep Expression That Excludes grep Itself

Another way to exclude our grep statement from the output is by using a regular expression within the command:

% ps | grep "[v]i"

This only finds our vi process:

 2592 ttys001    0:00.07 vi

This works because grep interprets the statement in the double-quotes as a regular expression. The square brackets are parsed into the regular expression logic of find the letter ‘v’ followed by the string ‘i’.

However, the command line of this grep as seen in the output of ps contains the square brackets, which are not matched by the regular expression. Put another way, grep vi contains the string “vi” that grep is looking for, but grep “[v]i” doesn’t contain the string “vi” in itself.

4. Conclusion

In this article, we walked through a couple of tools to narrow down a search by process name.

First, we saw how to use ps to find processes and how to use grep to narrow the search. Then, we looked at a couple of ways to exclude our grep command from the results.

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