70

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.

Pradep
  • 1,874
  • 5
  • 21
  • 31
  • 6
    If `pgrep` is available then you can just say `pgrep process_name`. [This post](http://unix.stackexchange.com/questions/225/pgrep-and-pkill-alternatives-on-mac-os-x) talks about `pgrep` availability on OSX – another.anon.coward Jul 18 '12 at 17:21

9 Answers9

103

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}'
Community
  • 1
  • 1
Vijay C
  • 1,179
  • 1
  • 7
  • 5
  • 2
    if the app is running this return correct PID but if that particular app is not running it return a number which not correct. i am on os x 10.9 – modusCell Apr 10 '14 at 22:22
  • 8
    If the app is not running, it might return the PID of the grep process. Take a look at my answer. – stepmuel Aug 10 '14 at 14:15
  • Just tested as pidof adb, nothing showed up. worked for some process though – Kristof Apr 25 '17 at 13:45
  • 1
    @mohacs @stepmuel to fix that do `ps -A | grep -m1 [f]irefox | awk '{print $1}'` – Motsel Mar 10 '19 at 21:14
  • 2
    many process are associated with helper processes that have the base process’s name as their prefix. for example process-helper, processd, processScriptRunner, etc. `-m1` will only select the first of all matches by pid, which may not be the one you want. `ps -A` output has columns pid, tty, time, and cmd. cmd includes the full command the process was executed with. use the `-c` flag to reduce the cmd column output to the process name only. add an end of line anchor to the grep search string to eliminate the helper processes from the output. `ps -Ac | grep -m1 ‘process$’ | awk '{print $1}'`. – pkfm Mar 24 '19 at 02:02
  • "If the app is not running, it might return the PID of the grep process" Exactly! I mean just try running this in your terminal without Firefox running, and it fails, and there's no prefix or whatever. It's just wrong! – DoctorPangloss Jul 05 '19 at 18:57
66

You can use the pgrep command like in the following example

$ pgrep Keychain\ Access
44186
bergercookie
  • 2,542
  • 1
  • 30
  • 38
62

You can install pidof with Homebrew:

brew install pidof
pidof <process_name>
hgascon
  • 751
  • 6
  • 4
7

This solution matches the process name more strictly:

ps -Ac -o pid,comm | awk '/^ *[0-9]+ Dropbox$/ {print $1}'

This solution has the following advantages:

  • it ignores command line arguments like tail -f ~/Dropbox
  • it ignores processes inside a directory like ~/Dropbox/foo.sh
  • it ignores processes with names like ~/DropboxUID.sh
stepmuel
  • 1,208
  • 12
  • 6
6

This is the shortest command I could find that does the job:

ps -ax | awk '/[t]he_app_name/{print $1}'

Putting brackets around the first letter stops awk from finding the awk process itself.

phatmann
  • 18,161
  • 7
  • 61
  • 51
  • 1
    From this answer (http://unix.stackexchange.com/a/74186): "By putting the brackets around the letter and quotes around the string you search for the regex, which says: Find the character 'f' followed by 'nord'. But since you put the brackets in the pattern 'f' is now followed by ']', so grep won't show up in the results list. Neat!" – phatmann Aug 08 '16 at 18:07
  • Very clever. Thanks for the explanation. – Floris Aug 08 '16 at 19:29
2

Try this one:

echo "$(ps -ceo pid=,comm= | awk '/firefox/ { print $1; exit }')"

The ps command produces output like this, with the PID in the first column and the executable name (only) in the second column:

bookworm% ps -ceo pid=,comm=
    1 launchd
   10 kextd
   11 UserEventAgent
   12 mDNSResponder
   13 opendirectoryd
   14 notifyd
   15 configd

...which awk processes, printing the first column (pid) and exiting after the first match.

Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124
1

You can try this

pid=$(ps -o pid=,comm= | grep -m1 $procname | cut -d' ' -f1)
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Slight improvements: to remove the path to the process so you don't get any accidental matches, and match all processes - use `pid=$(ps -ceo ...)`. – Nicholas Riley Jul 18 '12 at 17:27
  • I am not getting any response when i typed `echo "$(ps -ceo pid=,comm= | grep -m1 firefox | cut -d' ' -f1)"` in terminal. AM i doing something wrong ? – Pradep Jul 18 '12 at 17:36
  • the result of grep is ` firefox-bin 777`, 3 spaces, use `cut -d' ' -f3` – jackjr300 Jul 18 '12 at 19:32
  • The pid column is right justified, I didn't test this. I think I'll just put this in another answer :-) – Nicholas Riley Jul 18 '12 at 23:55
  • For me pid=$(ps aux -ceo pid=,comm= | grep -m1 "prl_naptd" | cut -w -f2) worked for macOS 13 – Bineesh Jul 10 '23 at 10:12
0

ps -o ppid=$(ps -ax | grep nameOfProcess | awk '{print $1}')

Prints out the changing process pid and then the parent PID. You can then kill the parent, or you can use that parentPID in the following command to get the name of the parent process:

ps -p parentPID -o comm=

For me the parent was 'login' :\

Fawntasia
  • 347
  • 2
  • 6
-2

Why don't you run TOP and use the options to sort by other metrics, other than PID? Like, highest used PID from the CPU/MEM?

top -o cpu <---sorts all processes by CPU Usage

Ape
  • 11