Getting PID of process in Shell Script Getting PID of process in Shell Script unix unix

Getting PID of process in Shell Script


Just grep away grep itself!

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


Have you tried to use pidof ABCD ?


It's very straight forward. ABCD should be replaced by your process name.

#!/bin/bashprocessId=$(ps -ef | grep 'ABCD' | grep -v 'grep' | awk '{ printf $2 }')echo $processId

Sometimes you need to replace ABCD by software name. Example - if you run a java program like java -jar TestJar.jar & then you need to replace ABCD by TestJar.jar.