Go Back

How to efficiently find and kill a ProcessID from the command line

Written by: Noel Vieira on Mon Aug 14 2023 00:00:00 GMT+0000 (Coordinated Universal Time)

Does having an application stuck or not working sound familiar? In this article, we’ll see how to find PID numbers and kill them all — at once.

I love my keyboard, but I don’t love, for instance, when it stops working upon reconnecting the docking station to the laptop. This trivial activity has turned into a sort of morning ritual that, if overlooked, could potentially consume 2 minutes of my life/day.

As developers, we do our best to avoid repeating ourselves on the one hand and to find ways to make our lives easier on the other. And let’s agree that opening whichever Activity Monitor you’re using, looking up the process, listing the PID numbers, and individually terminating each is neither an added-value activity nor an enjoyable experience.

There must be a better way…

CLI Tips and Tricks

One way of getting those two minutes back is by combining the pgrep command with the -f flag to list the process IDs that match the keyword passed as an argument. Let’s pretend my keyboard brand is called FooBar:


$ pgrep -f FooBar


pgrep -f will give you all the PID’s running on your machine.

Great! We managed to get all those PID numbers, so we are now in a good position to ditch that Activity Monitor. But now, what? Should we have to repeatedly type and kill each process manually, like cave people?

Don’t do this:


$ kill -9 93747 93817 93819


Do this instead:

$ kill -9 (pgrep -f FooBar)

#or

$ kill -9 `pgrep -f FooBar`

Command substitution is a powerful technique in which you wrap up a command with either backticks or parentheses to use its output as the input for the main (first) command. In this example, we pass the PID numbers from running the pgrep with the -f flag command as input arguments to kill -9.

Another good case for using command substitution is when you want to look up and kill a process running on a specific PORT.

Running the lsof command (reads ‘list open files’) with the -i flag outputs information on open files and processes.

$ lsof -i :3000

Adding the -t flag declutters the output and gives you the exact PID

$ lsof -t -i :3000

✅ So we can wrap it up with the backticks to look up and kill a PID in one command:

$ kill -9 (lsof -t -i :3000)

Effectively, running this combo will pass the PID of the process running on PORT 3000 as an argument to the kill -9 command, which in its turn will brute force its termination.

While command substitution is a powerful tool, it may be overkill for the specific use case — force quitting an application. So,

✅ You can also do this instead:

$ pkill -9 -f FooBar

By combining pkill with the -f flag, we leverage the power of looking up and killing running processes against a given pattern or keyword to kill them all at once.

Simple and straight to the point.

Hurrah 🎉 we got our 2 minutes back!

A huge shoutout to the folks at the Changelog podcast, particularly for the outstanding Efficient Linux at the CLI episode with Daniel J. Barrett, author of Efficient Linux at the Command Line.

Do you have any other tricks to accomplish similar results? I’d love to hear from you in the comments.

Go Back