The pkill command in Linux: send signals to processes by name

Introduction

In Linux system management, managing processes is a daily task. Sometimes it is necessary to stop, restart or send a specific signal to a group of processes that share the same name. The commandpkillallows it to be done quickly and safely, avoiding having to search the PID manually.

What's pkill?

pkillbelongs to the packageprocpsand works as a friendly interface ofkill. Instead of specifying the process number (PID), a pattern that matches the executable name or other attributes such as the user, terminal or group is indicated. Internally,pkillusepgrepto locate the PID and then send the indicated signal.

Basic syntax

The simplest way is:

pkill [options]

If the signal is omitted, it is sent by defaultSIGTERM(15). Some useful options are:

  • -u usuario: filter per owner.
  • -t tty: terminal filter.
  • -n: select the most recent matching process.
  • -o: select the oldest process.
  • -v: reverse the coincidence (excludes the pattern).
  • -f: matches the complete command line, not just the executable name.

Common signs

The signals can be indicated by number or by name. The most used inpkillare:

  • SIGTERM(15): request orderly completion.
  • SIGKILL(9): the process is immediately completed, without the possibility of capture.
  • SIGHUP(1): usually used to recharge daemon configurations.
  • SIGINT(2): simulates theCtrl + C.
  • SIGSTOP(19): stops the process; it can be continued withSIGCONT.

Practical examples

    • Finish all called processesfirefox:
pkill firefox
    • SendSIGHUPto thesshdof the useradmin:
pkill -u admin -HUP sshd
    • Stop the most recent process that coincides withbackup.sh:
pkill -n -f backup.sh
    • Restart a service by sendingSIGUSR1to all processes whose name containsnginx:
pkill -USR1 nginx

Cautions and best practices

Althoughpkillis powerful, its careless use can affect the system. It is recommended that:

  • First runpgrepwith the same pattern to see which PID will be affected.
  • Avoid using comodines too wide aspkill *; could try to kill critical processes.
  • In production environments, consider the use ofsudoonly when necessary and record the shares in a log.
  • Remember thatSIGKILLdoes not allow proper cleaning; use only whenSIGTERMIt didn't work.

Conclusion

The commandpkillsimplifies process signalling by name, saving time and reducing the possibility of errors when searching for PID manually. Knowing their syntax, the most common signals and applying good practices, any Linux administrator can manage processes efficiently and safely.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish