我们很多时候查进程都是用ps-ef|grep 进程名 命令,但是这样有一个弊端,就是每次会出来一个grep记录
如:
-
[root@test 1115]# ps -ef |grep crond
-
root 1662 1 0 21:09 ? 00:00:01 crond
- root 4492 2230 0 23:37 pts/1 00:00:00 grep crond
通常情况下我们会使用方法一:
-
[root@test 1115]# ps -ef |grep crond|grep -v grep
-
root 1662 1 0 21:09 ? 00:00:01 crond
- You have mail in /var/spool/mail/root
-
[root@test 1115]# ps -ef |grep "[c]rond"
- root 1662 1 0 21:09 ? 00:00:01 crond
大部分人可能会有这么一种误解:
ps -ef的时候是不会有grep crond这个进程的,为什么作为管道输出给grep crond 的时候会匹配到grep crond这么一个进程?
其实单纯执行ps -ef的时候确实不会有grep crond这个进程
但是ps -ef |grep crond时系统不会等ps -ef执行完后才去执行grep crond的,你可以理解为它们并发执行的
有人这么解释
管道本质上就是一段内存缓冲区,一个进程写数据进去,另一个进程从其中读取数据。
如果写进程把缓冲区写满了,读进程还没有从缓冲区中开始读数据,那么写进程就会挂在那里一直等,直到缓冲区的数据被取出来。
管道被设计出来是为了在进程间交换数据,所以管道两端的进程同时存在没什么不对。
好了,现在来说说为什么第二种方法能不匹配到grep本身进程
很简单,grep "[c]rond" 这个进程名字就叫grep "[c]rond"
你可以试试
- [root@test 1115]# echo 'grep "[c]rond"' |grep "[c]rond"
你看明白了么?