Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm able to to show the PID (2nd) column with the below command, but instead I want to show pwdx <PID> and also the COMMAND(8th) column. Thanks in advance, Chirag

ps -ef | grep runner | awk {'print$2'}
2432
6346
6348
6350
6352
7407
7410
7416
10824
17305
17494
29658
share|improve this question
1  
What is your real question? –  Hello71 Oct 19 '11 at 3:02

6 Answers 6

Instead of using awk to filter ps output, use the ps -o option to get what you want:

ps -e -o pid,comm

That should do what you describe above. See the ps man page for more things to print out.

UPDATE: I see I've failed to use pwdx. Try this on for size:

#!/bin/bash
ps -o pid= -o comm= |
while read LINE
do
    eval $(echo $LINE |
    awk '{pid=$1; comm=$2; printf "echo -n \"%s  \"; pwdx %s\n", comm, pid}' )
done

You may need to add flags to ps to get processes you want - the above only shows you your user ID's processes.

share|improve this answer
1  
The current directory (pwdx) is not part of the available columns with some if not all ps implementations. –  jlliagre Oct 19 '11 at 6:55

If you would like to print col 2 and 8 you just add $8 to the print.

ps -ef | grep runner | awk {'print $2" "$8'}
share|improve this answer

The following both work for me:

ps -ef | grep runner | awk {'print$2'} | xargs pwdx

and

ps -ef | grep runner | for i in `awk {'print$2'}`; do pwdx $i; done

FYI, you will get one that looks like: 3516: No such process which will correspond to your grep runner that will have completed by the time pwdx is called. (Also, for anyone using OS X, pwdx isn't in the default installation.)

EDIT: I just realized that you wanted to return COMMAND, too. This will show all three on one line for each process matching runner:

ps -ef | grep runner | for i in `awk {'print$2'}`; do echo `ps -e -o pid,comm | grep $i` `pwdx $i | awk {'print$2'}`; done
share|improve this answer

Here's how I would do it:

pwdx $(ps -ef | grep runner | awk {'print$2'})

alternate syntax:

pwdx `ps -ef | grep runner | awk {'print$2'}`
share|improve this answer
pwdx $(pgrep -f runner)|tr -d : > /tmp/1
pgrep -fl runner > /tmp/2
join /tmp/[12]
share|improve this answer
ps -e -o pid,comm | 
while read pid cmd; do 
  printf "%s %s %s\n" $pid $cmd "$(pwdx $pid| cut -d " " -f 2-)"
done

You might get some directories "No such process" if the process ends before pwdx is invoked for it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.