LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-07-2005, 01:58 PM   #1
RajRed
Member
 
Registered: Mar 2005
Posts: 66

Rep: Reputation: 15
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.
 
Old 09-07-2005, 03:02 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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

Last edited by Tinkster; 09-07-2005 at 03:13 PM.
 
Old 09-07-2005, 04:37 PM   #3
RajRed
Member
 
Registered: Mar 2005
Posts: 66

Original Poster
Rep: Reputation: 15
great!. it worked.

Thank you.
 
Old 09-07-2005, 06:44 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 12-09-2013, 09:42 AM   #5
kreezxil
LQ Newbie
 
Registered: Dec 2013
Posts: 1

Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by Tinkster View Post
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!
 
Old 12-09-2013, 10:08 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
Old 12-09-2013, 12:35 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...or shorter:
Code:
pgrep program_name
 
Old 12-09-2013, 12:43 PM   #8
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
..also
Code:
pidof program_name
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PID Kill Script or command ? Webmin rapture_1 Linux - General 1 11-02-2005 11:11 AM
'Command' field in Gnome launchers Comreak Linux - Software 7 06-05-2005 01:25 AM
How to extract the 16 byte source name field (Name to add) from the NetBIOS fram (NBF Bassam Linux - Networking 0 05-23-2004 04:58 AM
PID display command for kernel modules jedblack Linux - Software 1 11-28-2003 06:01 PM
how to get PID without using pidof command? ruchika Programming 2 09-22-2003 03:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration