52

Sometimes I notice that the process Python hung up. Then I'll kill it via the Activity Monitor and everything is fine.

I'd like to kill that process in the Terminal instead. However, looking up the PID seems kind of unecessary because there's only one process by the name Python.

Is it possible to kill a process by its unique name?

1
  • Also see this question if you're trying to kill it by the visible window name. Commented Jan 16, 2022 at 5:31

5 Answers 5

61

You should be able to run the command sudo killall Python.

You need to run as root because Python belongs to root, not the user.

5
  • 2
    I did that: No matching processes belonging to you were found
    – gentmatt
    Commented Mar 3, 2012 at 18:05
  • 1
    The Python process doesn't belong to your user. Try it as root instead: sudo killall Python.
    – HenningJ
    Commented Mar 3, 2012 at 18:09
  • 1
    @HenningJ Sudo worked for some reason. I just saw that the process does belong to me. I could kill the process without sudo when I killed it using it's PID. Why?
    – gentmatt
    Commented Mar 3, 2012 at 18:18
  • 1
    I'm thinking it's because using a PID isn't user/root specific, whereas a process name is. I'll edit this answer to reflect sudo.
    – Matt Love
    Commented Mar 3, 2012 at 18:23
  • This also worked for multiple identically named processes, nice
    – chrismarx
    Commented May 31, 2014 at 14:25
28
sudo killall -s SIGINT "process name"

If you can't be a sudo because it will ask for password and I feel you don't want that in a script. pkill come for rescue :)

pkill -9 "process name"
0
6

You can kill Applications by using Activity Monitor.app, being the GUI solution. That would be a simple "force quit". However, that doesn't always work out for different reasons in some situations!

The command-line solution as mentioned in the comment above holds a lot more options for the user. sudo killall Python or if it is a running program-process sudo killall /Applications/Whatever.app forces the the process to quit as well.

You can also force a process to quit, using its assigned PID. In the case of Activity Monitor.app it would be kill 25794 or kill -9 25794

enter image description here

Some more details:

The kill program in Terminal simply force quits a program, as though by remote control. (It even works when you SSH into your Mac from a remote location. Follow the kill command with the process ID number (short PID) of the program you want to terminate.

Unless you also use sudo, you can kill only programs you “own”—those running under your account. (The operating system itself—root—is always running programs of its own, and it’s technically possible that other people, dialing in from the road, are running programs of their own even while you’re using the Mac!)

The -9 flag is a “non-catchable, non-ignorable kill.” In other words, it’s an industrial-strength assassin that accepts no pleas for mercy from the program you’re killing.

6

Although it does use PID, I find the following to be pretty efficient:

ps aux | grep "String or name of process"

This line returns a host of information about the matching process(es), and you can kill or whatever from there.

5
pkill -f "process name"

Based on https://stackoverflow.com/a/22334807/1243763

2
  • 1
    Is there a pidof on macOS? And why not use pkill which doesn’t have the problem of the additional process?
    – nohillside
    Commented Mar 8, 2018 at 21:48
  • agree and I missed that. I had pidof binary as non-standard alias of ps -e -o pid,args | grep -E "\b$1\b" | awk "{print \$1}" | tr '\n' ' '. So, pkill -f makes more sense.
    – Samir
    Commented Mar 10, 2018 at 16:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .