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. |
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 |
great!. it worked.
Thank you. |
Quote:
Out of curiosity: which version did you end up using? :) Cheers, Tink |
Quote:
Thanks! |
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 |
...or shorter:
Code:
pgrep program_name |
..also
Code:
pidof program_name |
All times are GMT -5. The time now is 06:04 AM. |