We can view the process through the pgrep command, and we can signal the process through the pkill command.

View or Signal Processes By Name or Other Attributes

Here are some common options for both pgrep and pkill commands:

Option Description
-c or ‑‑count Output the number of processes found instead of the normal output.
-f or ‑‑full The pattern matches all columns.
-g or ‑‑pgroup pgrp,… Output the process corresponding to the group ID.
-G or ‑‑group gid,… Output the process corresponding to the real group ID.
-i or ‑‑ignore-case Case-insensitive matching.
-n or ‑‑newest Matches only the newly started processes.
-o or ‑‑oldest Matches only the earliest started processes.
-P or ‑‑parent ppid,… Matches only those processes whose parent processes IDs are the same as the specified IDs.
-s or ‑‑session sid,… Matches only processes with the same session IDs as the specified IDs.
-t or ‑‑terminal term,… Matches only processes that have the same control terminal as the specified terminal.
-u or ‑‑euid euid,… Matches only processes that have the same valid user IDs as the specified valid user IDs.
-U or ‑‑uid uid,… Matches only processes that have the same real user IDs as the specified real user IDs.
‑‑inverse Inverts the matching condition.
-x or ‑‑exact Matches only processes with the same name as the specified pattern, if -f option is specified that means matches the command line.
-F or ‑‑pidfile file Output the process IDs in the file, which is more useful for the pkill command.
-L or ‑‑logpidfile If the process IDs file is not locked, it will fail.
-r or ‑‑runstates D,R,S,Z,… Matches processes that are in the same state as the specified process state.
‑‑ns pid Matches the processes at the same namespaces.
‑‑nslist name,… Matches the processes that are at the specified namespaces, available ones are: ipc, mnt, net, pid, user, uts.

Here are some common options for pgrep command:

Option Description
-d or ‑‑delimiter delimiter A string used to separate the process number of the output.
-l or ‑‑list-name List both process name and process id.
-a or ‑‑list-full
-v Inverts the matching condition.
-w or ‑‑lightweight Output thread number instead of process number.

Here are some common options for pkill command:

Option Description
-signal or ‑‑signal signal

Examples

Output the Process Name of the Specified Process Name

1
pgrep systemd -l    # Output the first ten lines
img

Output the Process of the Specified User and the Full Command Line

1
pgrep -u root -a | head -10    # Output the first ten lines
img

Output the Process ID of the Specified Process Name

1
pidof systemd
img

References PGREP(1), PIDOF(1)

Buy me a coffeeBuy me a coffee