pkill Command in Linux



The pkill command in Linux sends signal processes based on their names or attributes such as user, group, or terminal. By default, it sends a SIGTERM signal to processes to terminate them. It can be case-sensitive and supports regular expressions (regex) for pattern matching. It is part of the procps (procps-ng) package.

Table of Contents

Here is a comprehensive guide to the options available with the pkill command −

Syntax of pkill Command

The syntax of the Linux pkill command is as follows −

pkill [options] [pattern]

In the above syntax, the [options] field is used to specify various options to modify how the matching is performed or specify which processes to target. The [pattern] field is used to specify the name or regular expression to match against the process name.

pkill Command Options

The options of the pkill command are listed below −

Flags Options Description
--signal signal Defines the signal to send to each matched process. Either numeric or symbolic signal names can be used
-c --count Suppress normal output; instead, print a count of matching processes
-d delimiter --delimiter delimiter Sets the string used to delimit each process ID in the output (default: newline)
-e --echo Display the name and PID of the process being killed
-f --full Match against the full command line instead of just the process name
-g pgrp,... --pgroup pgrp,... Match processes in the listed process group IDs
-G gid,... --group gid,... Match processes by their real group ID, using numeric or symbolic values
-i --ignore-case Perform case-insensitive matching
-l --list-name List the process name and process ID
-n --newest Select only the newest of the matching processes
-o --oldest Select only the oldest of the matching processes
-O secs --older secs Select processes older than the specified seconds
-P ppid,... --parent ppid,... Match processes by their parent process ID
-s sid,... --session sid,... Match processes by their session ID
-t term,... --terminal term,... Match processes by their controlling terminal
-u euid,... --euid euid,... Match processes by effective user ID
-U uid,... --uid uid,... Match processes by real user ID
-v --inverse Negate the matching
-w --lightweight Show all thread IDs instead of process IDs
-x --exact Match processes exactly by name or command line
-F file --pidfile file Read PIDs from a specified file
-L --logpidfile Fail if the specified pidfile is not locked
-r D,R,S,Z,... --runstates D,R,S,Z,... Match processes by their state
-A --ignore-ancestors Ignore all ancestors of the command
-H --require-handler Match processes with a userspace signal handler for the signal
--cgroup name,... Match processes by control group (cgroup) v2 name
--ns pid Match processes in the same namespaces. Requires root
--nslist name,... Match only the provided namespaces (e.g., ipc, mnt, net)
-q value --queue value Use sigqueue with an integer value for the signal
-V --version Display version information and exit
-h --help Display help and exit

Examples of pkill Command in Linux

This section demonstrates the usage of the pkill command in Linux with examples.

Killing a Process by Name

To kill a process by name, use the pkill command with the process name. For example, to kill the vim process, use the following command −

pkill vim
pkill Command in Linux1

Killing a Process by Name with Case-Insensitivity Enabled

To kill a process by name with case-insensitivity, use the -i or --ignore-case option −

pkill -i Vim
pkill Command in Linux2

Killing the Most Recent Process

To kill the most recently started process from the matching processes, use the -n or --newest option −

pkill -n firefox

The above command kills the newest firefox process based on its start time. If multiple firefox instances are running, it targets the one that was started most recently.

Killing the Oldest Process

To kill the oldest process among the matching process, use the -o or --oldest option with the process name −

pkill -o firefox

Sending a Specific Signal

To send a specific signal, specify the signal name with the process. For example, to send an HUP signal, use the following command −

pkill -HUP vim
pkill Command in Linux3

Other signals that can be used with the pkill command are listed below −

Signal Number Description
TERM 15 Gracefully terminate a process (default).
KILL 9 Forcefully terminate a process.
STOP 19 Pause a process.
CONT 18 Resume a paused process.

Note that not all processes are responsive to STOP and CONT signals.

Killing All Processes by a Group ID

Use the -G or --group option to kill all the processes by a group ID. For example, to kill processes associated with GID 994, use the following command −

pkill -G 994

To list the groups with their IDs, use the following command −

getent group

Killing All Processes of a Specific User ID

To kill all processes of a specific user, use the -U or --UID with the user ID −

pkill -U 1001

To list UIDs, use the following command −

getent passwd

To get the user ID of a specific user, use the id command with the user’s name −

id -u alex

Killing a Process by Parent Process ID

To kill a process by its parent PID, use the -P or --parent option −

pkill -P 4567

To get the parent process of a process, use the following command −

ps -o ppid= -p 4772
pkill Command in Linux4

Displaying a Killed Process Name and ID

To display the killed process name and ID, use the -e or --echo option with the pkill command −

pkill -e vim
pkill Command in Linux5

Killing Processes on a Specific Terminal

To kill processes associated with a specific terminal, use the -t or --terminal option with the terminal name.

pkill -t tty1

Displaying Usage Help

To display the usage help of the pkill command, use the -h or --help option:

pkill --help

Conclusion

The pkill command in Linux is a powerful tool for terminating processes based on various criteria such as process name, user, group, or terminal. It allows specifying different signals, matching processes using regular expressions, and filtering processes with several options like case-insensitive matching, listing processes, and targeting processes by specific attributes such as parent PID or group ID.

The pkill command provides an efficient way to manage processes without needing to identify each one manually. It is part of the procps package and is widely used for process management tasks.

Advertisements