我编写了一个脚本来检查在基于 OpenWrt 的系统中运行的进程实例数。如果我在终端中运行以下命令
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
root@SHAULA-720:~# echo $COUNT_PS
结果是
1
下面是 shell 脚本的代码,如果我运行这个脚本,结果是 4 而不是 1
#!/bin/ash
#for debug
ps -w | grep -v grep | grep upmpdcli
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
echo we have $COUNT_PS instances for upmpdcli;
logger we have $COUNT_PS instances for upmpdcli;
if [[ $COUNT_PS == 1 ]]; then
logger "we have only one instance"
#HERE PUT CODE TO START NEW PROCESS
elif [[ $COUNT_PS == 2 ]]; then
logger "we have 2 instances lets kill all and start a single"
kill -9 `pgrep upmpdcli`
elif [[ $COUNT_PS == 0 ]]; then
logger "we have no instance lets wait for cron to start it"
else
logger "we have $COUNT_PS instances"
fi
所以如果我运行/etc/upmpd-check.sh
结果we have 4 instances for upmpdcli
对我来说很奇怪。
我在这里想念什么?