25

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.

asked Jun 6, 2013 at 14:42

10 Answers 10

59

Just grep away grep itself!

process_id=`/bin/ps -fu $USER| grep "ABCD" | grep -v "grep" | awk '{print 2ドル}'`
answered Jun 6, 2013 at 14:45
Sign up to request clarification or add additional context in comments.

3 Comments

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
Surely you can think of something to "grep away" again, as the PID of your script, which should be available via the $$ variable
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 ....
39

Have you tried to use pidof ABCD ?

answered Jun 6, 2013 at 15:39

3 Comments

this is actually a better answer for most cases :)
Or pgrep ABCD
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.
10

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,36339 silver badges56 bronze badges
answered Feb 20, 2017 at 11:26

Comments

4

ps has an option for that:

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

Comments

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ドル}'
answered Jan 31, 2019 at 2:31

Comments

2

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.9k8 gold badges70 silver badges82 bronze badges
answered Nov 14, 2014 at 18:33

Comments

1

I found a better way to do this.

top -n 1 | grep "@#" | grep -Eo '^[^ ]+' 
answered May 3, 2021 at 6:30

Comments

0

ps | pgrep ABCD

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

answered Sep 16, 2020 at 12:18

Comments

0

You can use 'pgrep #prog_name' instead and it shall return prog_name PID directly.

answered Jan 10, 2024 at 20:41

Comments

0

If you got below,

$ ps -ef | grep test
root 1322 1 0 10:31:17 ? 00:00:38 sh /test.sh
root 9078 8593 4 18:17:24 pts/1 00:00:00 grep switch

then, try this.

$ echo $(pgrep -f test.sh)
1332
Alez
2,87710 gold badges29 silver badges38 bronze badges
answered May 30, 2024 at 9:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.