How To List And Kill Processes Using The PGrep And PKill Commands

Kill Linux Programs
Kill Linux Programs.

Introduction

There are lots of different ways to kill processes using Linux. For instance I previously wrote a guide showing "5 ways to kill a Linux program" and I have written a further guide called "Kill any application with a single command".

As part of the "5 ways to kill a Linux program" I introduced you to the PKill command and in this guide I will be expanding on the usage and available switches for the PKill command.

PKill

The PKill command allows you to kill a program simply by specifying the name. For instance if you want to kill all open terminals with the same process ID you can type the following:

pkill term

You can return a count of the number of processes killed by supplying the -c switch as follows:

pkill -c <programname>

The output will simply be the number of processes killed.

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

pkill -u <userid>

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

id -u <username>

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 <userid>

The real user ID is the ID of the user running the process. In most cases it will be the same as the effective user but if the process was run using elevated privileges then 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 <userid>


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

pkill -g <processgroupid>
pkill -G <realgroupid>

The process group id is the group id running the process whereas the real group id is the process group of the user who physically ran the command.

These may be different if the command was ran using elevated privileges.

To find the group id for a user run the following ID command:

id -g

To find the real group id using the following ID command:

id -rg

You can limit the amount of processes pkill actually kills. For instance killing all of a users processes is probably not what you want to do. But you can kill their latest process by running the following command.

pkill -n <programname>

Alternatively to kill the oldest program run the following command:

pkill -o <programname>

Imagine two users are running Firefox and you just want to kill the version of Firefox for a particular user you can run the following command:

pkill -u <uid> firefox

You can kill all processes which have a specific parent ID. To do so run the following command:

pkill -P <parentprocessID>

You can also kill all processes with a specific session ID by running the following command:

pkill -s <sessionID>

Finally you can also kill all processes running on a particular terminal type by running the following command:

pkill -t <terminal>

If you want to kill a lot of processes you can open a file using an editor such as nano and enter each process on a separate line. After saving the file you can run the following command to read the file and kill each process listed within it.

pkill -F /path/to/file

The Pgrep Command

Before running the pkill command it is worth seeing what the effect of the pkill command will be by running the pgrep command.

The pgrep command uses the same switches as the pkill command and a few extra ones.

Summary

This guide has shown you how to kill processes using the pkill command. Linux certainly has plenties of options available for killing processes including killall, kill, xkill, using the system monitor and the top command.

It is up to you to choose which one is suitable for you.

More From Us