• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

How to kill a process

Posted May 5, 2023 — Updated Jan 10, 2024

The easiest way: use pkill to kill a running process by name.

Let’s say you have a background task running:

a screenshot creating two background sleep tasks
This starts two background sleep tasks

Those sleep commands will run for 60 seconds and 600 seconds, respectively.

screenshot of "pgrep sleep"
Here are the two running process IDs

Kill a process by name

If we want to kill them sooner, we can just pkill them by name:

screenshot of pkill sleep
these two processes are now done

pkill works similar to killall—both commands will kill all matching processes. What if we only want to kill one?

Kill a process by ID

Maybe you have multiple processes running with similar names, and you only want to kill one of them. In that case, you can run kill {pid}.

In the example above, pgrep returned the process IDs for both sleep commands:

pgrep sleep results
Reposting the screenshot because who wants to scroll up?

To stop just one of them I could have run kill 26912, and left the other one untouched. But which PID is which? They’re going to run for different amounts of time, and I want to make sure I kill the right one.

How to find process IDs

In the screenshot above, pgrep didn’t differentiate between the two sleep commands; it just gave us the PIDs. The traditional way to find PIDs is via ps aux; specifically: ps aux | grep {name_to_look_for}.

However, I find pgrep -lf  to be a nicer option1:

screenshot running "pgrep -lf sleep"
This time we have 3 matching processes

Now we see the processes with their details, and can decide what to do from here.

What if pkill and kill don’t work?

By default, pkill and kill send processes a SIGTERM, which is the signal to terminate gracefully. Sometimes though, that doesn’t work. When it doesn’t, you may need to send a SIGKILL via kill -9 {PID} or pkill -9 {name}.

Sending SIGKILL does carry some risks, so you shouldn’t do it unless you know it’s ok. For more details, see this helpful article.


Helpful Links

  • The differences between pkill and killall – Stack Exchange
  • SIGTERM vs SIGKILL

Notes

  1. -l is “long output”, which gives the process name in addition to its PID e.g. 48289 sleep. When combining -l and -f we get the process’s full command e.g. 48289 sleep 100 ↩︎

Filed Under: Command Line

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy