LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how do I extract the pid field from ps aux command (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-extract-the-pid-field-from-ps-aux-command-361150/)

RajRed 09-07-2005 01:58 PM

how do I extract the pid field from ps aux command
 
I need to find the pid for the running program, so I did

"ps aux | grep program_name", it showed

root 12347 2.0 2.0 1588 244 pts/1 s+ Sep01 0.00 program_name
root 25317 0.0 0.0 1673 628 pts/5 s+ 13:25 0:00 grep program_name

Q1:How do I use grep to exclude "grep program_name" itself and only get the first line?

Q2: How do I extract the pid (i.e. 12347) from the first line combining the comamnds "ps aux" and "grep" in one single command line ?

Thanks.

Tinkster 09-07-2005 03:02 PM

ps aux | awk '/name/ { print $2}'

[edit]
And to answer your actual question: :}

ps aux | grep name | grep -v grep
and
ps aux | grep name | grep -v grep | awk '{print $2}'
[/edit]

Cheers,
Tink

RajRed 09-07-2005 04:37 PM

great!. it worked.

Thank you.

Tinkster 09-07-2005 06:44 PM

Quote:

Originally posted by RajRed
great!. it worked.

Thank you.

That's cool ...
Out of curiosity: which version did you end up using? :)


Cheers,
Tink

kreezxil 12-09-2013 09:42 AM

Quote:

Originally Posted by Tinkster (Post 1840672)
ps aux | awk '/name/ { print $2}'

[edit]
And to answer your actual question: :}

ps aux | grep name | grep -v grep
and
ps aux | grep name | grep -v grep | awk '{print $2}'
[/edit]

Cheers,
Tink

It has been 8 years going on 9 years since the above quote was made and I want to say it is as relevant today as it was then, saved me lots of work in making my minecraft server script even more robust and spot on as possible.

Thanks!

druuna 12-09-2013 10:08 AM

There's no need for awk, grep or other extra commands and pipes. The ps command can do this without any help:
Code:

ps -C program_name -o pid

unSpawn 12-09-2013 12:35 PM

...or shorter:
Code:

pgrep program_name

Madhu Desai 12-09-2013 12:43 PM

..also
Code:

pidof program_name


All times are GMT -5. The time now is 06:04 AM.