检测脚本是否已经在运行的时候发现的问题,简化一下场景,以下脚本
#!/bin/sh
ps -eF | grep $0 | grep -v grep
在 CPU 占用比较高的时候,会看到如下结果:
[root@hostname opt]# ./test.sh
root 2011 24865 0 1709 1096 1 17:33 pts/2 00:00:00 /bin/sh ./test.sh
root 2013 2011 0 1709 440 2 17:33 pts/2 00:00:00 /bin/sh ./test.sh
root 2014 2011 0 1709 256 3 17:33 pts/2 00:00:00 /bin/sh ./test.sh
在 CPU 空闲的时候就只能看到一条自身的进程。另外如果脚本中不做 grep :
#!/bin/sh
ps -eF
在执行时加上 grep ,也只能看到自身:
[root@hostname opt]# ./test.sh |grep test.sh |grep -v grep
root 19741 24865 0 1709 1108 0 17:39 pts/2 00:00:00 /bin/sh ./test.sh
请教是什么原因?