首页 > bash while true 时,为什么一个实例会产生多个进程?

bash while true 时,为什么一个实例会产生多个进程?

test.sh文件内容如下

#!/bin/sh
while true
do
    PROBE_PID=$(ps| grep -v grep | grep "udhcpc" | sed -n  '1P' | awk '{print $1}')
    echo $?;
    echo ${PROBE_PID}
done

当我执行这个脚本的时候 ./test.sh

通过 ps | grep test.sh| grep -v grep 发现有多个 test.sh 进程存在,不知道是什么原因导致的,这是执行几次这个命令的输出。

root@diancan-test:~# ps | grep test.sh| grep -v grep
 1951 root      1360 S    {test.sh} /bin/sh ./test.sh
 2972 root      1360 S    {test.sh} /bin/sh ./test.sh
root@diancan-test:~# ps | grep test.sh| grep -v grep
 1951 root      1360 S    {test.sh} /bin/sh ./test.sh
root@diancan-test:~# ps | grep test.sh| grep -v grep
 1951 root      1360 S    {test.sh} /bin/sh ./test.sh
 4415 root      1360 S    {test.sh} /bin/sh ./test.sh
root@diancan-test:~# ps | grep test.sh| grep -v grep
 1951 root      1360 S    {test.sh} /bin/sh ./test.sh
root@diancan-test:~# ps | grep test.sh| grep -v grep
 1951 root      1360 S    {test.sh} /bin/sh ./test.sh
 4796 root      1360 S    {test.sh} /bin/sh ./test.sh
root@diancan-test:~# ps | grep test.sh| grep -v grep
 1951 root      1360 S    {test.sh} /bin/sh ./test.sh
 4949 root      1360 S    {test.sh} /bin/sh ./test.sh

如果脚本中只是 while true的话,不会有这样的现象,我觉得问题出现在:

$(ps| grep -v grep | grep "udhcpc" | sed -n  '1P' | awk '{print $1}')

这个命令上。


搜索关键字 subshell

【热门文章】
【热门文章】