Using the pkill command in the Linux Terminal

This article covers the basics of commands pkill on Linux.

pkill It is a command line utility that sends an indication of the currently running program process based on certain criteria. Processes can be identified by their full or partial name, the user running the process, or other attributes.

ranking pkill It is part of the package procps (or procps-ng), which comes pre-installed on most Linux distributions. pkill is the command associated with pgrep which just prints the list of matches.

How to use the pkill command

The syntax of the pkill command is as follows:

pkill [OPTIONS] <PATTERN>

Matching <PATTERN> Designed with Extended regular expressions.

When connected without any options, pkill sends a signal 15 (TERM) to the PIDs of all running programs that match the specified name. For example, to kill all Firefox processes, you can run:

pkill -9 firefox

The command returns output 0 When at least one operation matches the requested name. Otherwise, the exit code is 1. This can be useful when used in shell scripts.

To send a different signal to the corresponding process, call the command pkill with options --signalfollowed by numeric or names of symbolic signs. Another way to send a signal is to run pkill followed by a sign name or number preceded by a hyphen (-).

Use commands kill -l To list all available signals.

The most commonly used signals are:

  • 1 (-HUP): To restart the operation.
  • 9 (-kill): To kill a process.
  • 15 (-TERM): To stop the process in a different way.

Signals can be identified in three different ways:

  • using a number (eg, -1)
  • prefixed with “SIG” (eg, -SIGHUP)
  • Without the “SIG” prefix (eg, -HUP).
See also  How to install and use Curl on CentOS 8

For example, to force a Nginx process to be reloaded, you could run:

pkill -HUP nginx

pkill useregular expressions to match the process name. It’s always a good idea to use commands pgrep To print matches before sending a signal to them. For example, to list all processes that contain the word “sshin his name:

1039 sshd
2257 ssh-agent
6850 ssh
31279 ssh-agent

If you want to send signals only to processes whose name exactly matches the search pattern, you can use:

pkill '^ssh$'

caret (^) will watch the game at the beginning of the string, and the dollar $ in the end.

By default, pkill Matches only the process name. when the option -f The command will search for a match with the full list of arguments. If the command contains spaces, quote the entire command:

pkill -9 -f "ping 8.8.8.8"

Use options -u to know pkill To show processes run by a specific user:

pkill -u toni

To select multiple users, separate their names with commas:

pkill -u budi,toni

You can also combine search options and styles. For example to print out all the processes and their names that run under the username “Dear” and have the word “gnome” in their name, you can type:

pkill -9 -u budi gnome

To display only previously started (most recently) or most recent (recently) processes, use the options -n (is the latest or first to run) or -o (For the oldest or most recent fugitive).

For example, to kill the most recently created screen app:

pkill -9 -n screen

conclusion

ranking pkill It is used to send signals to running programs based on various parameters.

See also  How to install and configure Ansible AWX on CentOS 8

For more information about the pkill command, visit the pkill manual page or type man pkill at your station.

Source link