How to get the PID of a process by giving the process name in Mac OS X ?

Pradep picture Pradep · Jul 18, 2012 · Viewed 113.6k times · Source

I am writing a script to monitor the CPU and MEM of any given process. For that i need to send in the name of the process to be monitored as a commandline argument. For example.

./monitorscript <pname>

I need to get the pid of the process in the script so that i can use a ps -p <pid> inside.

How do i get the pid of a process given its process name?

I understand that there might be multiple processes in the same name. I just want to get the first process out of that list.

Answer

Vijay C picture Vijay C · Jul 18, 2012

The answer above was mostly correct, just needed some tweaking for the different parameters in Mac OSX.

ps -A | grep [f]irefox | awk '{print $1}'