Site icon DesignLinux

Pkill Command in Linux

Pkill Command in Linux

The pkill is a command-line utility, used to send signals to the processes of a running program. The processes can be specified by their names, by a user name, or other attributes. In this article we will discuss about pkill command in Linux.

By default the pkill command comes pre-installed as a part of the procps package on most of Linux distributions.

Use of pkill Command

The basic syntax for the pkill command is as follows:

pkill [OPTIONS] [PATTERN]

The matching [PATTERN] is specified using extended regular expressions.

If you run the pkill command without any option, it sends the 15 (TERM) signal to the PIDs of all running programs that match with the given name. For example, to stop the all processes of the Google Chrome, you would run:

pkill -9 google-chrome

It will returns 0 as exit code if any one running process match otherwise it will returns 1. Exit codes are useful while you are writing shell scripts.

To send the different signals to the matched process use the --signal option followed by the signal number or name.

To get the list of all available signals use kill -l command:

Following are the most common signals:

Signals can be specified in three different ways:

For example, to reload the Nginx processes you would run:

pkill -HUP nginx

pkill uses regular expressions to match the processes names. It is always a good idea to use the pgrep command to print the matched processes before sending signals to them.

For example, to list all processes that contain “ssh” in their names:

2045 sshd
2154 ssh-agent
8506 ssh
1259 ssh-agent

If you want to send a signal only to the processes which names are exactly as the search pattern, you would use:

pkill '^ssh$'

pkill command matches the process name by default. You would use -f option to match against full argument lists.

pkill -9 -f "ping 8.8.8.8"

Use -u option with pkill to match processes being run by a specified user:

pkill -u kunj

Specify the multiple users by separating their names with commas:

pkill -u kunj,tecnstuff

You can combine options and search patterns. For example to send KILL signal all processes that run under user “kunj” and contains “gnome” in their names you would type:

pkill -9 -u kunj gnome

Use the -n option to get most recent and -o for oldest started process.

For example, to kill the most recently created screen:

pkill -9 -n screen

Conclusion

This article shows you how to use the pkill command to send signals to running programs based on different criteria. To learn more about the pkill command, take a look at pkill man page.

Feel free to leave a comment if you have any question or feedback.

Exit mobile version