Common Linux Commands – pkill: Send Signals to Processes by Name or Other Attributes

Click the business card below to follow the public account and star it to receive my latest shares.Reply in the background with Embedded Learning Materials to get a learning package👇👇👇

Introduction

pkill stands for “process kill” and is used to terminate processes. It allows you to send signals to one or more processes by their name or other attributes without manually looking up the process ID (PID). The pkill command is very useful when you need to terminate processes in bulk or based on specific conditions. Its principle is to match the process name based on a given pattern and then send the specified signal (default is SIGTERM) to the matched processes.

Usage

pkill [options] pattern

Common Options

  • <span>-<signal>, --signal <signal></span>: Specify the signal to send to each matching process. You can use either the numeric value or symbolic name of the signal.
  • <span>-c, --count</span>: Suppress normal output and print the number of matching processes instead. If no processes match (i.e., none are found), the command will return a non-zero value.
  • <span>-e, --echo</span>: Display the <span>PID</span> or name of the terminated processes.
  • <span>-f, --full</span>: Normally, the search pattern only matches the process name. With this option, the search pattern matches the full command line.
  • <span>-g, --pgroup pgrp,...</span>: Only match processes belonging to the specified process group ID. Process group ID 0 specifically refers to the process group of the pkill command itself.
  • <span>-G, --group gid,...</span>: Only match processes whose real group ID is in the specified list. You can use either the numeric value or symbolic name of the group ID.
  • <span>-i, --ignore-case</span>: Ignore case when matching processes.
  • <span>-n, --newest</span>: Among the matched processes, only select the one with the most recent start time.
  • <span>-o, --oldest</span>: Among the matched processes, only select the one with the earliest start time.
  • <span>-O, --older secs</span>: Select processes that started more than the specified <span>secs</span> seconds ago.
  • <span>-P, --parent ppid,...</span>: Only match processes whose parent process ID is in the specified list.
  • <span>-s, --session sid,...</span>: Only match processes whose session ID is in the specified list. Session ID 0 specifically refers to the session ID of the <span>pkill</span> command itself.
  • <span>-t, --terminal term,...</span>: Only match processes whose controlling terminal is in the specified list. The terminal name should not include the <span>/dev/</span> prefix.
  • <span>-u, --euid euid,...</span>: Only match processes whose effective user ID is in the specified list. You can use either the numeric value or symbolic name of the user ID.
  • <span>-U, --uid uid,...</span>: Only match processes whose real user ID is in the specified list. You can use either the numeric value or symbolic name of the user ID.
  • <span>-x, --exact</span>: Only match processes whose name (if the <span>-f</span> option is used, match the command line) exactly matches the search pattern.
  • <span>-F, --pidfile file</span>: Read the <span>PID</span> from the specified file <span>file</span>.
  • <span>-L, --logpidfile</span>: If the <span>PID</span> file (see <span>-F</span> option) is not locked, the command will fail.
  • <span>-r, --runstates D,R,S,Z,...</span>: Only match processes in the specified states.
  • <span>--ns pid</span>: Match processes that belong to the same namespace as the specified <span>PID</span>. To match processes of other users, you need to run as <span>root</span>.
  • <span>--nslist name,...</span>: Only match specified types of namespaces. Available namespace types include:<span>ipc</span>, <span>mnt</span>, <span>net</span>, <span>pid</span>, <span>user</span>, <span>uts</span>.
  • <span>-V, --version</span>: Display version information.
  • <span>-h, --help</span>: Display help information.

Pattern

This operand is used to specify an extended regular expression to match process names or command lines.

Return Values

  • <span>0</span>: Successfully matched and sent signals to one or more processes.
  • <span>1</span>: No processes matched or could not send signals to them.
  • <span>2</span>: Command line syntax error.
  • <span>3</span>: Fatal error, such as insufficient memory.

Reference Examples

Terminate all processes named test

pkill test

Terminate processes based on the full command line

 pkill -f "top -d 5"

Only terminate processes started with the <span>top -d 5</span> command line.

Ignore case when matching processes

pkill -i TOP

Using the <span>-i</span> option will ignore case differences in the given name, so in this example, it will also terminate the <span>top</span> process.

Display the names and PIDs of processes to be terminated

pkill -e top

Using the <span>-e</span> option will output the names and PIDs of the terminated processes, and if termination fails, it will also display the failure information:

jay@jaylinuxlenovo:~/test$ pkill -e top
pkill: killing pid 727 failed: Operation not permitted
pkill: killing pid 169325 failed: Operation not permitted
top killed (pid 699820)

Display the number of processes to be terminated

pkill -c top

Using the <span>-c</span> option displays the number of processes matched by <span>pkill</span>, even if termination fails, it will still count in the final number:

jay@jaylinuxlenovo:~/test$ pkill -c top
pkill: killing pid 727 failed: Operation not permitted
pkill: killing pid 169325 failed: Operation not permitted
4

Notes

  • The count when using the <span>-c</span> option refers to the number of matched processes, not the number of processes that successfully received the signal.
  • By default, the process name used for matching is limited to the first 15 characters displayed in <span>/proc/pid/stat</span>. If you need to match the full command line, use the <span>-f</span> option (which matches the content in the <span>/proc/pid/cmdline</span> file).
  • Zombie processes will also be matched.

Common Linux Commands - pkill: Send Signals to Processes by Name or Other Attributes

Common Linux Commands – killall: Terminate Processes by Name

Common Linux Commands - pkill: Send Signals to Processes by Name or Other Attributes

Why does it still show undefined errors even though the header file is included?

Common Linux Commands - pkill: Send Signals to Processes by Name or Other Attributes

When storing data in internal FLASH, you must know ……

Common Linux Commands - pkill: Send Signals to Processes by Name or Other Attributes

Is there a way to determine if the APP is valid in the Bootloader?

Common Linux Commands - pkill: Send Signals to Processes by Name or Other Attributes

A single line of code almost overturned my colleague’s worldview!!!

Leave a Comment