And*_*nko 4 unix bash grep ps nano
我需要使用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
Run Code Online (Sandbox Code Playgroud)
但是,当我将此命令与参数$ 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
Run Code Online (Sandbox Code Playgroud)
这对我有用,但是实际上,恕我直言,此解决方案不是最佳解决方案。我在grep名称之后使用阴影列命令,并打印除命令外的所有列。
您始终可以使用两阶段方法。
1.)找到通缉PID犯。为此使用最简单的方法ps
ps -o pid,comm | grep "$2" | cut -f1 -d' '
Run Code Online (Sandbox Code Playgroud)
将ps -o pid,comm只打印两列,如:
67676 -bash
71548 -bash
71995 -bash
72219 man
72220 sh
72221 sh
72225 sh
72227 /usr/bin/less
74364 -bash
Run Code Online (Sandbox Code Playgroud)
因此,轻轻松松很容易(且无噪音,没有错误触发)。在cut刚刚提取的PID。例如
ps -o pid,comm | grep bash | cut -f1 -d' '
Run Code Online (Sandbox Code Playgroud)
版画
67676
71548
71995
74364
Run Code Online (Sandbox Code Playgroud)
2),现在您可以使用该标志将找到的内容提供PIDs给另一个,因此完整的命令为:ps-p
ps -o uid,pid,ppid,ni,vsz,rss,stat,tty,time,command -p $(ps -o pid,comm | grep bash | cut -f1 -d' ')
Run Code Online (Sandbox Code Playgroud)
输出
UID PID PPID NI VSZ RSS STAT TTY TIME COMMAND
501 67676 67675 0 2499876 7212 S+ ttys000 0:00.04 -bash
501 71548 71547 0 2500900 8080 S ttys001 0:01.81 -bash
501 71995 71994 0 2457892 3616 S ttys002 0:00.04 -bash
501 74364 74363 0 2466084 7176 S+ ttys003 0:00.06 -bash
Run Code Online (Sandbox Code Playgroud)
例如使用的解决方案$2是
ps -o uid,pid,ppid,ni,vsz,rss,stat,tty,time,command -p $(ps -o pid,comm | grep "$2" | cut -f1 -d' ')
Run Code Online (Sandbox Code Playgroud)