LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 02: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, 04:02 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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 04:13 PM.
 
Old 09-07-2005, 05:37 PM   #3
RajRed
Member
 
Registered: Mar 2005
Posts: 66

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

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

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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, 10: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, 11:08 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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, 01:35 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
...or shorter:
Code:
pgrep program_name
 
Old 12-09-2013, 01:43 PM   #8
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 502

Rep: Reputation: 137Reputation: 137
..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 12:11 PM
'Command' field in Gnome launchers Comreak Linux - Software 7 06-05-2005 02: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 05:58 AM
PID display command for kernel modules jedblack Linux - Software 1 11-28-2003 07:01 PM
how to get PID without using pidof command? ruchika Programming 2 09-22-2003 04:12 PM


All times are GMT -5. The time now is 04:56 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration