How to Use the PGrep and PKill Commands

The easiest way to kill processes using Linux

There are five ways to kill a Linux program, and you can kill any application with a single command. Here's an explanation of the usage and available switches for the PGrep and the PKill command in Linux.

PGrep

For those familiar with the Linux command line, the grep command should be familiar. It filters through text to find a specific word or set of characters. PGrep is similar, but for system processes. Use PGrep to find the process number of a problematic program that you want to stop.

If Firefox, for example, isn't responding and you need to shut it down, use PGrep to search for firefox. The command displays the process or processes that Firefox is running.

pgrep firefox
Linux PGrep

This isn't super useful most of the time because PKill can kill a process with the application name. However, if you don't know or remember the exact name, PGrep matches part of it. So, to use the Firefox example, when you search for fire to display the processes for Firefox, you get other processes with fire in the name.

pgrep fire

Be careful when you type. There usually isn't any overlap, if you're careful what you search.

The results come out the same way, and you can use the process number to stop the stalled application.

PKill

The PKill command kills a program by specifying the name. For example, if you want to kill all open terminals with the same process ID, type the following:

pkill term

To return a count of the number of processes killed, supply the -c switch as follows:

pkill -c

The output is the number of processes killed.

Linux PKill number

To kill all the processes for a particular user, run the following command:

pkill -u

To find the effective user id for a user, use the ID command as follows:

id -u

For example:

id -u gary

You can also kill all the processes for a particular user using the real user ID as follows:

pkill -U

The real user ID is the ID of the user running the process. In most cases, it is the same as the effective user. However, the process was run using elevated privileges, the real user ID of the person running the command and the effective user will be different.

To find the real user ID, use the following command:

id -ru

You can also kill all the programs in a particular group by using the following commands:

pkill -g
pkill -G