Linux pkill Command: Signal Processes by Name

pkill sends a signal to processes that match a name or condition. The default is SIGTERM, which requests termination, but if the name matches broadly, it can affect multiple unintended processes.

It uses selection criteria like pgrep, but instead of displaying the results, it sends a signal to the target. Therefore, it is safe to first run pgrep -a with the same criteria to verify the target user and command line.

Basic syntax

pkill [option] pattern

The installed implementation and options may vary depending on the distribution. Check the current system description with man pkill.

Examples

First, verify the target

Review whether the process name matches exactly.

pgrep -a -x myworker

Default termination request

Send SIGTERM to processes that match the reviewed name.

pkill -x myworker

Limit scope by user

If processes with the same name are running under multiple accounts, specify the user together.

pkill -u alice -x myworker

Checking the Result of a Termination Command

pkill sends a signal to processes matching a name pattern. Even if it succeeds, it usually does not print a list of PIDs, so first run pgrep with the same criteria to verify the target.

pgrep -a -x demo-worker
pkill -TERM -x demo-worker
pgrep -a -x demo-worker

The initial query may show test processes, sending the signal itself produces no output, and the final query will be empty if termination is complete. Sending a signal does not guarantee an immediate termination, so check again after a short while.

-x restricts the match to the full name. Be especially careful when using -f to search the entire command line, as the pattern may match more widely. Apply the examples only to the test processes you created and follow operational procedures for actual services.

Main Options and Format

Options/Format Description
-x Select processes that exactly match the full name.
-u user Limit the target to valid user IDs.
-f Check the entire command line as the target.
-TERM Explicitly send SIGTERM.
-e Display information about the target to which the signal was sent in supported implementations.

Precautions when using

pkill -f can select multiple tasks if only part of the command line matches. When managing services, prioritize the normal shutdown procedure of service management tools whenever possible. SIGKILL skips cleanup tasks, so it is not usually used as the first choice.

Frequently Asked Questions

What is the difference between pkill and pgrep?

pgrep finds PIDs that match the criteria, while pkill actually sends signals to processes that match the criteria.

Official Documentation

You can check the exact behavior of the options and differences depending on the implementation in the official documentation related to pkill.