How to use the command pkill (with examples)
pkill is a command used to signal and terminate processes based on their name or command. It provides a simple way to stop processes without having to manually find and kill them one by one.
Use case 1: Kill all processes which match
Code:
pkill "process_name"
Motivation: This use case is useful when there are multiple processes with the same name running on a system and all of them need to be terminated. Instead of finding and killing each process individually, pkill can kill all processes matching the given name at once.
Explanation:
- “process_name”: This argument specifies the name of the process to be killed. Use quotes around the process name to ensure it is interpreted as a whole string.
Example output:
If we want to kill all instances of the process “nginx”, we would run pkill "nginx"
. This command will terminate all running processes with the name “nginx”.
Use case 2: Kill all processes which match their full command instead of just the process name
Code:
pkill -f "command_name"
Motivation: Sometimes a process can have multiple instances with different arguments and options. If we want to terminate all instances of a process with a specific command line, we can use this use case.
Explanation:
- “-f”: This option tells pkill to match the full command line instead of just the process name.
- “command_name”: This argument specifies the exact command line to match and kill.
Example output:
Let’s say we want to terminate all instances of a process that is running the command “python my_script.py”. We can use the command pkill -f "python my_script.py"
. This will find and kill all processes whose command line matches “python my_script.py”.
Use case 3: Force kill matching processes
Code:
pkill -9 "process_name"
Motivation: This use case is helpful when a process is unresponsive or is not terminating with a regular kill signal. The “-9” option sends a SIGKILL signal, which forcefully terminates the process.
Explanation:
- “-9”: This option sends a SIGKILL signal to the matching processes, ensuring they are terminated irrespective of their current state.
- “process_name”: This argument specifies the name of the process to be force killed.
Example output:
If we want to forcefully terminate all instances of a process named “java”, we would run pkill -9 "java"
. This command will send the SIGKILL signal to all running processes with the name “java”.
Use case 4: Send SIGUSR1 signal to processes which match
Code:
pkill -USR1 "process_name"
Motivation: Certain processes may be designed to respond to specific signals. The “-USR1” option sends the SIGUSR1 signal to the matching processes, allowing them to execute a custom action or behavior.
Explanation:
- “-USR1”: This option sends the SIGUSR1 signal to the matching processes, which they may interpret and respond to accordingly.
- “process_name”: This argument specifies the name of the processes to receive the SIGUSR1 signal.
Example output:
Suppose we want to send the SIGUSR1 signal to all instances of a process called “custom_service”. We can use the command pkill -USR1 "custom_service"
. This will send the SIGUSR1 signal to all running processes with the name “custom_service”.
Use case 5: Kill the main firefox process to close the browser
Code:
pkill --oldest "firefox"
Motivation: If there are multiple instances of a browser running and we want to close the browser completely, we can use this use case to kill the oldest process, which is typically the main process responsible for running the browser.
Explanation:
- “–oldest”: This option tells pkill to kill the oldest process that matches the given name instead of killing all matching processes.
- “firefox”: This argument specifies the name of the process to be killed.
Example output:
Let’s say we have multiple instances of the “firefox” browser running, and we want to close the browser completely. Running pkill --oldest "firefox"
will kill the oldest process running the “firefox” browser, resulting in the closure of the entire browser.
Conclusion:
pkill is a powerful command for terminating processes based on their name or command. It provides a convenient and efficient way to stop multiple processes with a single command, saving time and effort. Knowing the different use cases of pkill allows users to effectively manage and control running processes on a system.