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

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.


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}'

You can use the pgrep command like in the following example

$ pgrep Keychain\ Access
44186

You can install pidof with Homebrew:

brew install pidof
pidof <process_name>