6

I am able to to show the PID (2nd column) with the below command:

ps -ef | grep runner | awk {'print$2'}
2432
6346
6348
6350
6352
7407
7410
7416
10824
17305
17494
29658

But instead I want to show pwdx <PID> and also the COMMAND (8th column).

How can I do this?

closed as unclear what you're asking by G-Man, Kusalananda, GAD3R, Archemar, Michael Homer Jan 16 '17 at 2:12

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What is your real question? – Hello71 Oct 19 '11 at 3:02
8

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.

  • 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
4

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'}
1

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'}`
0

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
0
pwdx $(pgrep -f runner)|tr -d : > /tmp/1
pgrep -fl runner > /tmp/2
join /tmp/[12]
0
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.

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