1

I know about pwdx, but it would be really nice if top showed the PWD: in other words, I want to see PWD and CPU/mem usage side by side. Does anyone have a script or one-liner to combine the output from top/ps with pwdx with periodic refresh?

1
  • You can combine top, pwdx, and awk to generate such a report.
    – DopeGhoti
    Jul 13, 2017 at 17:51

2 Answers 2

1

Here's my abominable solution:

ps --no-headers -e -o pid,%cpu,%mem,args | grep -v "ps --no-headers" > ps.txt && awk '{ print $1 }' ps.txt > pids.txt && cat pids.txt | xargs pwdx | cut -d ' ' -f 2 > pwd.txt && awk '{ print " "$1" "$2" "$3" "}' ps.txt > ps1.txt && cut -d ' ' -f 4- ps.txt > ps2.txt && paste ps1.txt pwd.txt ps2.txt

Can it be done without writing files? Or just generally less nightmarishly?

1

I've managed to make it a little less nightmarish:

#!/bin/bash
ps --no-headers -e -o pid,%cpu,%mem,args > ps_out.txt
awk '!/--no-headers/ { print $1 }' ps_out.txt | tee pids.txt | xargs pwdx 2>/dev/null > paths.txt
sed --in-place 's/://' paths.txt
awk 'FILENAME=="paths.txt" {pids[$1][0]=$2} FILENAME=="ps_out.txt" { pid=$1; $1=""; $0=$0; pids[pid][1]=$0} END {for( pid in pids ) {print pid, pids[pid][1], pids[pid][0]} }' paths.txt ps_out.txt
rm pids.txt paths.txt ps_out.txt

To make that awk script slightly more readable:

FILENAME=="paths.txt" {
   pids[$1][0]=$2
}
FILENAME=="ps_out.txt" {
   pid=$1;
   $1="";
   $0=$0;
   pids[pid][1]=$0
}
END {
   for( pid in pids ) {
      print pid, pids[pid][1], pids[pid][0]
   }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .