Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用grep过滤进程_Linux - Fatal编程技术网

Linux 使用grep过滤进程

Linux 使用grep过滤进程,linux,Linux,我如何使用grep过滤底层流程 root 1626 1 0 04:49 ? 00:00:00 /usr/sbin/sshd USERNAME 5133 5038 0 14:12 pts/0 00:00:00 /bin/bash ./find_proc.sh sshd USERNAME 5137 5133 0 14:12 pts/0 00:00:00 grep sshd 是什么让我得到了输出。我知道你需要使用grep-v来过滤掉它,但

我如何使用grep过滤底层流程

root      1626     1  0 04:49 ?        00:00:00 /usr/sbin/sshd
USERNAME    5133  5038  0 14:12 pts/0    00:00:00 /bin/bash ./find_proc.sh sshd
USERNAME  5137  5133  0 14:12 pts/0    00:00:00 grep sshd
是什么让我得到了输出。我知道你需要使用grep-v来过滤掉它,但是我不确定这样做的顺序。它接受的参数是$1。因此,我在终端中的输入是./script\u file sshd,以获得上面的输出。有什么建议吗

ps -ef | grep "$1"  

没有给我输出

那么问题是您正在运行类似于
ps aux | grep sshd
,而
grep
最终发现自己在进程列表中?有一个简单的技巧可以解决这个问题,即将一个字符括在括号中:

ps -ef | grep "$1" | grep -v grep  

[d]
grep
以与
d
相同的方式解释,但它仍然是命令行的一部分,因此它不包括字符串
sshd

,所以问题是您正在运行类似
ps aux | grep sshd
,并且
grep
最终发现自己在进程列表中?有一个简单的技巧可以解决这个问题,即将一个字符括在括号中:

ps -ef | grep "$1" | grep -v grep  

[d]
grep
以与
d
相同的方式解释,但它仍然是命令行的一部分,因此它不包括字符串
sshd

,您可以使用
pgrep
。它可用于大多数现代Linux和Unix系统(在OSX上可通过
自制
):


您可以使用
pgrep
。它可用于大多数现代Linux和Unix系统(在OSX上可通过
自制
):


谢谢@DavidW.:我仍然使用较旧的OSX 10.6.8


雪豹有一个类似于
pgrep
pill
的命令。在许多方面,
killall
优于
pgrep
pkill
,但使用起来也有点复杂

其中一个参数是
-s
参数,该参数仅显示其功能

killall
的主要问题是,如果你不小心,它可能会导致死亡:

pgrep -fl "$1"

谢谢@DavidW.:我仍然使用较旧的OSX 10.6.8


雪豹有一个类似于
pgrep
pill
的命令。在许多方面,
killall
优于
pgrep
pkill
,但使用起来也有点复杂

其中一个参数是
-s
参数,该参数仅显示其功能

killall
的主要问题是,如果你不小心,它可能会导致死亡:

pgrep -fl "$1"

为什么不使用cut或awk等命令istead of grep?为什么不使用cut或awk等命令istead of grep?
pgrep
是一种方法。它不会导致必须从grepped输出中过滤掉
grep
的问题。
pgrep
pkill
都包含在小牛的OSX中。就像ps-ef | pgrep-fl“$1”一样。谢谢@DavidW。我仍然使用较旧的
OSX 10.6.8
@user2860658:不使用
ps-ef
。只需使用
pgrep-fl“$1”
pgrep
即可。它不会导致必须从grepped输出中过滤掉
grep
的问题。
pgrep
pkill
都包含在小牛的OSX中。就像ps-ef | pgrep-fl“$1”一样。谢谢@DavidW。我仍然使用较旧的
OSX 10.6.8
@user2860658:不使用
ps-ef
。只需使用pgrep-fl“$1”