【发布时间】:2017-04-13 17:08:03
【问题描述】:
我需要使用键入名称的进程的 ps 打印 UID PID PPID PRI NI VSZ RSS STAT TTY TIME 列。
GNU nano 2.0.6
File: file2
ps o uid,pid,ppid,ni,vsz,rss,stat,tty,time | grep $2 > $1
cat $1
echo "enter pid of process to kill:"
read pid
kill -9 $pid
但是当我使用这个带有参数 $2 = bash 的命令时它什么也没有打印出来(这个过程存在)
更新
GNU nano 2.0.6
File: file2
ps o uid,pid,ppid,ni,vsz,rss,stat,tty,time,command | grep $2 | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9}' > $1
cat $1
echo "enter pid of process to kill:"
read pid
kill -9 $pid
这对我有用,但实际上这个解决方案恕我直言并不是最好的。我使用影子列命令,在 grep 名称之后并打印除命令之外的所有列。
【问题讨论】:
-
您错过了
comm列(comm- 只是命令名称 - 最适合grep bash,cmd- 带参数的命令,grep bash会失败) -
您可以使用
cut -d',' -f-9,而不是长的awk命令。更好的是,要避免awk/cut,可以使用ps o uid,pid,(...),tty,time $(pgrep $2)