Site icon LinuxCommands.site

linux pkill/pgrep commands tutorial: pkill/pgrep syntax and examples

In operating systems such as Ubuntu and MAC, pkill and pgrep provide help manuals as a set of commands, which can be viewed through the man command.

pgrep, pkill – look up or signal processes based on name and other attributes

pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout.

pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout.

Syntax

pgrep [options] pattern
pkill [options] pattern

Options

Examples

List all processes whose names contain SSH and their process numbers

➜  ~ pgrep -l ssh
778 sshd
1859 ssh-agent
3560 sshd
3607 sshd
12717 sshd
12774 sshd
13314 sshd
13368 sshd
13513 sshd
13571 sshd

# The following command can achieve the same effect of pgrep.

➜  ~ ps -ef | grep 'ssh' | grep -v grep  | awk '{print $2, $8}'

List the processes with the smallest process ID in the SSH process name

➜  ~ pgrep -lo ssh
778 sshd

You can usually use this method to view the main process ID of a web server (such as nginx, httpd).

Kill all processes with a given partial name

➜  ~ pkill -f man

Of course, you can also use the following combined commands.

➜  ~ ps -ef | grep "man" | grep -v grep | awk '{print $2}' | xargs kill

Kill all processes of the specified user

➜  ~ pkill -u ylspirit

Kill all processes that do not belong to root

➜  ~ pkill -vu root

Kick user

➜  ~ pkill -kill -t pts/0

Kill all processes of the specified terminal

Exit mobile version