How to efficiently find and kill a ProcessID from the command line
Tags: productivity, bash, linux
Published: 2023-08-14 17:42:12

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 to get 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:

```bash
$ pgrep -f FooBar
```

pgrep -f <keyword> will give you all the PIDs 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:

```bash
$ kill -9 38759 38761 38762 38769 38776 38798
```

✅ Do this instead:

```bash
$ 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.

```bash
$ lsof -i :3000
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd7yqmyecnczo5qng3x5.png)

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

```bash
$ lsof -t -i :3000
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlv98cs9x4bgmf723ahf.png)

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

```bash
$ 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 run a simpler alternative:

```bash
$ pkill -9 -f WhatsApp
```

By combining `pkill` with the `-f` flag, we leverage the power of looking up for running processes against a matching 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.