24

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.

Mayank Jain
  • 2,504
  • 9
  • 33
  • 52

8 Answers8

56

Just grep away grep itself!

process_id=`/bin/ps -fu $USER| grep "ABCD" | grep -v "grep" | awk '{print $2}'`
blue
  • 2,683
  • 19
  • 29
  • Thanks for quick Answer.It worked.Also what if i dont want PID of script which is executing this. Because my script also contains ABCD – Mayank Jain Jun 06 '13 at 14:49
  • Surely you can think of something to "grep away" again, as the PID of your script, which should be available via the `$$` variable – blue Jun 06 '13 at 14:57
  • 3
    A trick for avoiding the 2nd grep is to give `grep` a regular expression that matches the same thing, but appears different on the command line: `... | grep "[A]BCD" | awk ...`. – chepner Jun 06 '13 at 17:28
33

Have you tried to use pidof ABCD ?

wizard
  • 1,456
  • 1
  • 11
  • 20
  • 6
    this is actually a better answer for most cases :) – blue Oct 23 '15 at 12:13
  • 1
    Or `pgrep ABCD` – Kevin Campion Sep 20 '18 at 19:51
  • 1
    The `pidof` command does not behave the same on every Linux distribution. So using `pidof` is not a good solution. It is also necessary to distinguish between the version of `ps` included in BusyBox - in lightweight Linux distributions (for example in a set-top box) and another `ps`, which is designed for full-fledged Linux. In the BusyBox version of the `ps` command, almost no arguments / switches work. In the case of BusyBox, the `ps` command is used a little differently. You have to be careful. – s3n0 Mar 20 '22 at 13:29
9

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

#!/bin/bash

processId=$(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.

Alex Zavatone
  • 4,106
  • 36
  • 54
A M S Rejuan
  • 447
  • 6
  • 12
2

ps has an option for that:

process_id=`/bin/ps -C ABCD -o pid=`
2

You can also do away with grep and use only awk.
Use awk's expression matching to match the process name but not itself.

/bin/ps -fu $USER | awk '/ABCD/ && !/awk/ {print $2}'
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
1

You can use this command to grep the pid of a particular process & echo $b to print pid of any running process:

b=`ps -ef | grep [A]BCD | awk '{ printf $2 }'`
echo $b
Sébastien Le Callonnec
  • 26,254
  • 8
  • 67
  • 80
1

ps | pgrep ABCD

You can try the above command to return the process id of the ABCD process.

Leenu
  • 11
  • 3
1

I found a better way to do this.

top -n 1 | grep "@#" | grep -Eo '^[^ ]+' 
Varun Nair
  • 11
  • 2