brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: pkill -x only kills processes whose name matches exactly

Linux
~/tips/tip-shell-pkill-x-exact-name

pkill -x node

Stops processes by exact name, so killing 'vim' does not also take down 'gvim' or 'vimdiff'.

pkill matches patterns, and that is the problem: pkill node also hits every process with node somewhere in its name. The -x flag makes it precise.

pgrep -a -x node
pkill -x node

With -x the pattern has to match the process name exactly. Add -f and it has to match the whole command line, so pkill -x -f 'python3 app.py' only matches when the entire command line is exactly that.

Run pgrep -a with the same options first. It lists what would match, with the command line, and nothing is killed. That is the safe order: look, then shoot.

By default pkill sends SIGTERM, which asks a program to stop politely. Only reach for -9 when that does not work, because SIGKILL gives the program no chance to clean up.

The caveat is the name length. Without -f, the name is the 15 characters from /proc/pid/stat, so a longer program name can fail to match with -x. In that case use -f with the full command line. Also remember that pkill can only signal processes you are allowed to signal, so your own, unless you run it as root.

Further reading: pkill(1) manual, procps, pgrep(1) manual

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link