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:

pkill [options] <pattern>
  • 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 of pkill.

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:

pkill firefox

Sample Output:

[no output]
  • This command will send the SIGTERM signal to all processes whose name matches firefox. This will ask the firefox 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 the SIGKILL signal, which forcefully kills the process.

Example:

pkill -9 firefox

Sample Output:

[no output]
  • This sends the SIGKILL signal to all processes matching the name firefox, 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 contain fire, you can use a partial string.

Example:

pkill fire

Sample Output:

[no output]
  • This will terminate all processes with names that contain fire, including firefox, 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:

pkill -u john

Sample Output:

[no output]
  • This will send the default SIGTERM signal to all processes owned by the user john.

5. Terminating Processes by Group Name

  • You can use the -g option to kill processes by their process group.

Example:

pkill -g 1234

Sample Output:

[no output]
  • This will send the SIGTERM signal to all processes in the group with ID 1234.

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:

pkill -l firefox

Sample Output:

firefox firefox-bin
  • This lists all processes matching the name firefox (or a pattern containing firefox).

7. Sending Custom Signals

  • You can use the -s option to specify a custom signal to send to the matched processes.

Example:

pkill -s SIGUSR1 firefox

Sample Output:

[no output]
  • This sends the SIGUSR1 signal to all processes with the name firefox, 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:

pkill -t tty1

Sample Output:

[no output]
  • This will send the default signal (SIGTERM) to all processes running on tty1.

Summary of pkill Options:

OptionDescription
-9Send 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.
-lList all processes matching the pattern without killing them.

Example of pkill in Action:

  1. Terminate all firefox processes:

    pkill firefox
  2. Forcefully terminate all chrome processes:

    pkill -9 chrome
  3. Terminate all processes owned by the user john:

    pkill -u john
  4. List all processes that contain python in their name:

    pkill -l python

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.