How to get process id from process name?

The problem with your second example is that the $1 is in single quotes, which prevents bash from expanding the variable. There is already a utility that accomplishes what you want without manually parsing ps output.

pgrep "$1"

Your ps -cl1 output looks like this:

  UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S             ADDR TTY           TIME CMD
  501   185   172      104   0  31  0  2453272   1728 -      S    ffffff80145c5ec0 ??         0:00.00 httpd
  501   303     1 80004004   0  31  0  2456440   1656 -      Ss   ffffff8015131300 ??         0:11.78 launchd
  501   307   303     4004   0  33  0  2453456   7640 -      S    ffffff8015130a80 ??         0:46.17 distnoted
  501   323   303 40004004   0  33  0  2480640   9156 -      S    ffffff80145c4dc0 ??         0:03.29 UserEventAgent

Thus, the last entry in each line is your command. That means you can use the full power of regular expressions to help you.

The $ in a regular expression means the end of the string, thus, you could use $ to specify that not only does the output must have Skype in it, it must end with Skype. This means if you have a command called Skype Controller, you won't pull it up:

ps -clx | grep 'Skype$' | awk '{print $2}' | head -1

You can also simplify things by using the ps -o format to just pull up the columns you want:

ps -eo pid,comm | grep 'Skype$' | awk '{print $1}' | head -1

And, you can eliminate head by simply using awk's ability to select your line for you. In awk, NR is your record number. Thus you could do this:

ps -eo pid,comm | grep 'Skype$' | awk 'NR == 1 {print $1}'

Heck, now that I think of it, we could eliminate the grep too:

ps -eo pid,comm | awk '/Skype$/  {print $1; exit}'

This is using awk's ability to use regular expressions. If the line contains the regular expression, 'Skype$', it will print the first column, then exit

The only problem is that if you had a command Foo Skype, this will also pick it up. To eliminate that, you'll have to do a bit more fancy footwork:

ps -eo pid,comm | while read pid command
do
    if [[ "$command" = "Skype" ]]
    then
        echo $pid
        break
    fi
done

The while read is reading two variables. The trick is that read uses white space to divide the variables it reads in. However, since there are only two variables, the last one will contain the rest of the entire line. Thus if the command is Skype Controller, the entire command will be put into $command even though there's a space in it.

Now, we don't have to use a regular expression. We can compare the command with an equality.

This is longer to type in, but you're actually using fewer commands and less piping. Remember awk is looping through each line. All you're doing here is making it more explicit. In the end, this is actually much more efficient that what you originally had.


If pgrep is available on Mac, you can use pgrep '^Skype$'. This will list the process id of all processes called Skype.

You used the wrong quotes in your script:

ps -clx | grep "$1" | awk '{print $2}' | head -1

or

pgrep "^$1$"

Tags:

Macos

Bash

Grep

Awk