Getting PID of process in Shell Script

Mayank Jain picture Mayank Jain · Jun 6, 2013 · Viewed 79.7k times · Source

I am writing one shell script and I want to get PID of one process with name as "ABCD". What i did was :

process_id=`/bin/ps -fu $USER|grep "ABCD"|awk '{print $2}'`

This gets PID of two processes i.e. of process ABCD and the GREP command itself what if I don't want to get PID of GREP executed and I want PID only of ABCD process?

Please suggest.

Answer

blue picture blue · Jun 6, 2013

Just grep away grep itself!

process_id=`/bin/ps -fu $USER| grep "ABCD" | grep -v "grep" | awk '{print $2}'`