我知道pwdx
,但如果显示 PWD,那就太好了top
:换句话说,我想并排查看 PWD 和 CPU/mem 使用情况。有没有人有一个脚本或单行代码可以将top
/的输出ps
与pwdx
定期刷新结合起来?
答案1
这是我的令人讨厌的解决方案:
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
不写文件可以吗?或者只是一般不那么噩梦般?
答案2
我设法让它不再那么噩梦般:
#!/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
为了使该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]
}
}