1

This question is similar in spirit to How can I kill a process by its name?, but not a duplicate for the reason below.

As an example, here's how IntelliJ appears in Activity Monitor:

enter image description here

The name is "IntelliJ IDEA". This is also the name visible when you toggle windows. However ps aux will show the process name as idea because that's the name of the process being executed:

/Applications/IntelliJ IDEA.app/Contents/MacOS/idea

Typing pkill -i intellij does nothing, as you have to type pkill -i idea to actually kill the right process. But is there a way to do this without having to lookup the right process name first? I'd like to type in kill_by_visible_name intellij and see the process terminated.

One option is to run ps aux | grep -i intellij but this will also find processes that happen to run from a folder named "IntelliJ", not just the actual IntelliJ process. This will also fail for processes where the visible name is not the same as the name of the application folder. Another option is to lookup the PID within Activity Monitor, but this is time consuming when you already know the name of the application.

2
  • What is ‘Process Explorer’? A web search finds only a MS Windows program. Do you mean the macOS Activity Monitor? (If so, can't you just stop it from there, or show the PID and kill that directly?)
    – gidds
    Jan 15 at 22:59
  • @gidds yes, Activity Monitor, updated. I can kill it that way, but often I know the visible name of the process and its faster for me to open the terminal than to bother with GUI. Jan 15 at 23:11

1 Answer 1

4

Figured out this is possible using lsappinfo:

#!/bin/bash
if [ -z "$1" ]
then
      echo "Please provide a process name"
      exit
fi
apps_list=$(lsappinfo)
found_line_numbers=$(echo "$apps_list"  | grep -nE '([0-9]+\) "[a-zA-Z ]+")' | grep -i "$1" | awk '{print $1}' FS=":")
if [ -z "$found_line_numbers" ]
then
      echo "Process named $1 not found"
else
      while IFS= read -r line; do
        line_pid=$(echo "$apps_list" | tail -n +$line | head -n 5 | grep pid | awk -F ' ' '{print $3}')
        echo "Trying to kill process $line_pid"
        kill -9 "$line_pid"
      done <<< "$found_line_numbers"
fi

The grep/awk combo is a bit awkward, but I couldn't find a better to extract the PID from lsappinfo.

3
  • lsappinfo | grep -iw -A 2 $1 | awk '/pid = / { print $3 }'. Still a bit risky IMHO, the grep part may match the wrong entries.
    – nohillside
    Jan 30 at 10:08
  • Might be safer to do something like lsappinfo | grep -iw -A 1 'executable path.*'"$1" ...
    – nohillside
    Jan 30 at 10:19
  • @nohillside fixed that part now, thanks. Feb 4 at 22:49

You must log in to answer this question.

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