Linux pkill command
The pkill
command in Linux is used to send signals to processes based on their name, user, group, or other attributes. It is a more user-friendly alternative to the kill
command when you want to send signals to processes based on their names or patterns rather than their process IDs (PIDs).
Basic Syntax:
pattern
: The name or pattern of the process you want to target. It is typically the name of the process (or a pattern) you want to match.options
: Various options that can be used to modify the behavior ofpkill
.
Common Usage of pkill
1. Terminating a Process by Name
- The most common use of
pkill
is to terminate processes by their name. It will match the name of the process or a part of it and send the specified signal (default:SIGTERM
) to those processes.
Example:
Sample Output:
- This command will send the
SIGTERM
signal to all processes whose name matchesfirefox
. This will ask thefirefox
processes to terminate gracefully. If there are multiple instances of Firefox running, they will all be terminated.
2. Forcefully Terminating a Process
- If the process does not terminate gracefully, you can use the
-9
option to send theSIGKILL
signal, which forcefully kills the process.
Example:
Sample Output:
- This sends the
SIGKILL
signal to all processes matching the namefirefox
, forcing them to terminate immediately without any cleanup.
3. Terminating a Process by Partial Name Match
pkill
can match partial names. For example, if you want to terminate all processes with names that containfire
, you can use a partial string.
Example:
Sample Output:
- This will terminate all processes with names that contain
fire
, includingfirefox
,firewalld
, etc. It is a case-insensitive match by default.
4. Terminating Processes for a Specific User
- You can use the
-u
option to target processes belonging to a specific user.
Example:
Sample Output:
- This will send the default
SIGTERM
signal to all processes owned by the userjohn
.
5. Terminating Processes by Group Name
- You can use the
-g
option to kill processes by their process group.
Example:
Sample Output:
- This will send the
SIGTERM
signal to all processes in the group with ID1234
.
6. Listing Processes Without Killing Them
- The
-l
option can be used to list all processes that match the given pattern without actually killing them.
Example:
Sample Output:
- This lists all processes matching the name
firefox
(or a pattern containingfirefox
).
7. Sending Custom Signals
- You can use the
-s
option to specify a custom signal to send to the matched processes.
Example:
Sample Output:
- This sends the
SIGUSR1
signal to all processes with the namefirefox
, which could be handled by the application in a custom manner (e.g., reload configuration or perform a specific action).
8. Terminating Processes by Terminal
- The
-t
option allows you to kill processes running on a specific terminal (tty).
Example:
Sample Output:
- This will send the default signal (
SIGTERM
) to all processes running ontty1
.
Summary of pkill
Options:
Option | Description |
---|---|
-9 | Send SIGKILL signal to forcefully terminate processes. |
-u <username> | Target processes owned by the specified user. |
-g <group_id> | Send signal to all processes in the specified process group. |
-s <signal> | Send a custom signal to the processes. |
-t <tty> | Send a signal to processes running on the specified terminal. |
-l | List all processes matching the pattern without killing them. |
Example of pkill
in Action:
Terminate all
firefox
processes:Forcefully terminate all
chrome
processes:Terminate all processes owned by the user
john
:List all processes that contain
python
in their name:
Conclusion
The pkill
command is a powerful tool for managing processes in Linux. It provides a convenient way to send signals to processes by their name, user, terminal, or other attributes, without needing to manually find their PID. It's particularly useful for terminating multiple instances of a process or performing targeted actions based on process characteristics.