linux pkill

This article describes the basics of the Linux pkill command.

pkill is a command-line program that can be sent to a running process based on a specified standard signal. A process can be specified by its full or partial name, the user running the process, or other attributes.

The pkill command is part of the procps (or procps-ng) package, which is pre-installed in almost all Linux distributions. pkill is a wrapper for the pgrep program, which prints a list of matching processes only.

How to use the pkill command

The syntax of the pkill command is as follows.

1
pkill [OPTIONS] <PATTERN>

The matching <PATTERN> is specified using an extended regular expression.

When called without any options, pkill sends the 15 (TERM) signal to the PIDs of all running programs matching the specified name. For example, to stop all Firefox processes normally, you can run:

1
pkill -9 firefox

The command returns 0 when at least one running process matches the requested name. Otherwise, the exit code is 1. This is useful when writing shell scripts.

To send a different signal to the matching process, call the pkill command with the -signal option, followed by a numeric or symbolic signal name. Another way to send signals is to run pkill followed by the signal name or a number starting with a hyphen (-).

Use the kill -l command to list all available signals.

The most commonly used signals are.

  • 1 (HUP): reloads the process.
  • 9 (KILL): kills the process.
  • 15 (TERM): Stops the process normally.

Signals can be specified in three different ways.

  • using a number with the “SIG” prefix (e.g. -SIGHUP) (e.g. -1)
  • without the “SIG” prefix (e.g. -HUP) ).

For example, to reload the Nginx process, run:

1
pkill -HUP nginx

pkill uses regular expressions to match process names. It is a good idea to use the pgrep command to print the matching processes before sending them the signal. For example, list all processes that have “ssh” in their name.

1
2
3
4
1039 sshd
2257 ssh-agent
6850 ssh
31279 ssh-agent

If you only want to send signals to processes whose names are identical to the search pattern, you can use that:

1
pkill '^ssh$'

The insertion sign (^) character is at the beginning of the string, and the dollar $ is at the end of the string.

By default, pkill matches only the process name. When the -f option is used, the command will match the full list of arguments. If the command contains spaces, use quotes to enclose the entire command.

1
pkill -9 -f "ping 8.8.8.8"

Use the -u option to tell pkill to match the process that the given user is running.

1
pkill -u mark

To specify multiple users, separate their names with commas.

1
pkill -u mark,danny

You can also combine options and search patterns. For example, to send the KILL signal, run under the user “mark” and enter all processes with “gnome” in their name.

1
pkill -9 -u mark gnome

To display only the most recently (oldest) or recently (newest) started processes, use the -n (for newest) or -o (for oldest) option.

For example, to kill the most recently created screen.

1
pkill -9 -n screen

Conclusion

The pkill command is used to send signals to running programs according to different criteria.

For more information about the pkill command, please visit the pkill man page or type man pkill in the terminal.