I have many processes matching a pattern. How do I kill them all at once?

Quick Linux Tip #53:

Try: pkill -f 'python.*worker.py'

Info: pkill matches processes using a pattern. The -f option matches against the full command line, making it useful when multiple processes run the same script with different arguments. Always preview matching processes with pgrep -af before terminating them.

Examples:

  • $ pgrep -af 'ffmpeg'  # Preview matching processes and full arguments
  • $ pkill -9 -f 'stuck-process'  # Force-kill matching processes
  • $ pkill -u linuxteck  # Terminate processes owned by a user

Note: Always preview matches with pgrep before using pkill. The default signal is SIGTERM (15), which gives processes an opportunity to exit cleanly. Use -9 (SIGKILL) only when a process does not respond to normal termination. Be especially careful with broad patterns because pkill -f can match more processes than expected.




LinuxTeck.com
linuxteck@ubuntu:~$ pgrep -af 'python.*worker.py'
3456 python3 /opt/app/worker.py --queue high
3457 python3 /opt/app/worker.py --queue medium
3458 python3 /opt/app/worker.py --queue low

linuxteck@ubuntu:~$ pkill -f 'python.*worker.py'

linuxteck@ubuntu:~$ pgrep -af 'python.*worker.py'
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #52: My rsync is saturating my connection. How do I cap its bandwidth?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.