Introduction
pkill
is a command-line utility that sends signals to a running program's processes based on specified criteria. The processes can be identified by their full or partial names, the person who is operating them, or other characteristics.
The pkill
command comes pre-installed on nearly all Linux distributions as part of the procps
(or procps-ng
) package. pkill
is essentially a wrapper for the pgrep
software that only prints a list of processes that match.
In this tutorial, you will learn about pkill
command. We will also address a few FAQs on pkill command in Linux.
How to Use the pkill
Command
pkill
command has the following syntax:
pkill [OPTIONS] <PATTERN>
Extended regular expressions are used to specify the matching <PATTERN>
.
The pkill
delivers the 15
(TERM
) signal to the PIDs of all running programs that match the provided name when executed without any options. To gracefully shut down all Firefox processes, for example, run:
pkill -15 firefox
When at least one running process has the same name as requested, the command returns 0
. Otherwise, 1
is the exit code. When building shell scripts, this can come in handy.
Invoke the pkill
command with the --signal
option, followed by either the numeric or symbolic signal name, to deliver a different signal to the matching processes. Running pkill
followed by the signal name or number prefixed by a hyphen (-
) is another way to transmit a signal.
To get a list of all accessible signals, use the kill -l
command.
The following are the most regularly used signals:
- To reload a process, use
1
(HUP
). 9
(KILL
): to terminate a procedure.15
(TERM
): to end a process gently.
Signals can be defined in one of three ways:
- without the "SIG" prefix (e.g., -SIGHUP)
- using a number (e.g., -1)
- with the "SIG" prefix (e.g., -HUP).
To refresh the Nginx processes, for example, type:
pkill -HUP nginx
To match the process's names pkill
employs regular expressions. Before sending signals to matched processes, it's always a good practice to run the pgrep
command to print them out. To list all processes with "ssh" in their names, for example:
1039 sshd
2257 ssh-agent
6850 ssh
31279 ssh-agent
If you only want to deliver a signal to processes whose names match the search pattern exactly, use the following command:
pkill '^ssh$'
pkill
only matches the process name by default. The command matches complete argument lists when the `-f `option is given. Quote the whole command if the command contains spaces:
pkill -9 -f "ping 8.8.8.8"
To inform pkill
to match processes run by a certain user, use the -u
option:
pkill -u mark
To specify several users, use commas to separate their names:
pkill -u mark,danny
You can also mix and match search patterns and options. To send a KILL
signal to all processes that run under the user "mark" and have "gnome" in their names, type:
pkill -9 -u mark gnome
Use the -
n (for newest) or -o
(for oldest) options to display only the processes that were started the least recently (oldest) or the most recently (newest).
To remove the most recently produced screen, for example:
pkill -9 -n screen
FAQs on pkill Command in Linux
How does pkill differ from kill?
While the kill command requires you to know and specify the process ID (PID) of the process to be terminated, pkill can match processes based on their names or other attributes, making it easier to kill multiple processes simultaneously.
How can I terminate a process using pkill?
To terminate a process using pkill, you need to specify its name or other attributes that uniquely identify it. For example, pkill firefox
will terminate all processes with the name "firefox".
Can pkill send signals other than terminating processes?
Yes, pkill can send various signals to processes, not just termination signals. It allows you to send any signal using the -signal
option, such as -HUP
for hangup or -USR1
for user-defined signal 1.
What if multiple processes match the pattern provided to pkill?
If multiple processes match the pattern provided to pkill, it will send the specified signal to all the matching processes, terminating them or performing the desired action.
Is it possible to use regular expressions with pkill?
Yes, you can use regular expressions with pkill to match processes. By using the -f
option, you can specify a regular expression to match the command line associated with processes.
Can root terminate any process using pkill?
Yes, the root user can terminate any process using pkill. However, non-root users can only terminate the processes they own or have sufficient permissions to manipulate.
Can pkill be used with a username instead of a process name?
Yes, pkill can be used with a username instead of a process name. By using the -u
option followed by the username, it will terminate all processes owned by that user.
Conclusion
The pkill
command sends signals to running processes based on a variety of criteria.
Visit the pkill
man page or type man pkill
in your terminal for more information about the pkill
command.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.