Killing Multiple Instances of a Process on Mac OS X
In this post, I’ll outline a couple ways to kill multiple running instances of a process on OS X.
A handy program to have is pidof, which is a utility that returns the process identifier (PID) of a running process or processes.
Since it’s not available by default on Mac, you can use homebrew:
brew install pidof
Now, you can easily find the PIDs for a process, for example to see the PIDs associated with all running instances of nginx, run:
pidof nginx
To kill all running instances of nginx use:
kill $(pidof nginx)
Sometimes using pidof won’t work because the processes don’t have an accurate name associated with them. An example is if you’re running a program with a runtime such as java.
In this case you can first find the processes with something like:
ps aux |grep tomcat
You can exclude the grep process and pipe them to awk and kill them:
kill `ps aux | grep tomcat | grep -v grep | awk '{print $2}'`
or
kill `ps aux | grep '[t]omcat' | awk '{print $2}'`