On unix-like OS, the pkill
command sends a signal to the processes while the pgrep
command searches for processes currently running on the system. Originally, the pkill
was written for using with the Solaris 7 (1998). kill, killall, pkill, pgrep can be used on all the GNU/Linux OS, MacOS X and Linux shell on Windows 10. The kill signals HUP (1) reloads a process, KILL (9) kills a process, TERM (15) gracefully stop a process. This signal (HUP) was used in the days of dial up modems to let the process know that the serial line had been Hung UP. That convention is been maintained still today.
Both the pgrep and pkill programs were introduced in Sun Solaris. Both of them accepts a pattern as argument to match against the names of running processes. pgrep just prints a list of matching processes but pkill will send the intended signal to the processes. The killall command differs from pkill in that, it exactly matches the argument name. The killall command has options for matching processes by age/time. The killall command on the UNIX systems kills all processes by shutting down if run by root.
You can run the top
command to see the currently running processes and run our example commands. The syntax for the pkill command is :
---
1 | pkill [OPTIONS] <PATTERN> |
Example :
1 2 | pkill -HUP nginx pkill -1 nginx |
As pkill
uses regular expressions to match, using the pgrep
or pkill
command to get a list of the matched processes is practical :
1 | pgrep 'zsh$' |
xkill
is the simplest program to kill a non-responding program.
Actually we can run a longer command to return the process identifier of a named task:
1 | ps ax | awk '{sub(/.*\//, "", $5)} $5 ~ /bash/ {print $1}' |
The above is equivalent to :
1 | pgrep 'zsh$' |
We can use pgrep
to list all the processes which does not belong to a mentioned user (like root
) :
1 | pgrep -v -u root |
pgrep is generally most useful when used in combination with pkill.
Tagged With can i kill a program with pgrep , how to use pgrep -l , how to use pkill command in windows 10 , pgrep equivalent windows , pkill for windows bash? , pkill pattern -f , pkill windows 10 , windows pgrep