本文介绍了使用Bash检查进程是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码检查进程是否正在运行:
I am trying to check if a process is running with the code below:
SERVICE="./yowsup/yowsup-cli"
RESULT=`ps aux | grep $SERVICE`
if [ "${RESULT:-null}" = null ]; then
echo "not running"
else
echo "running"
fi
但是它一直在回显它正在运行,尽管不是.我意识到grep本身就是结果,这就是问题所在.
But it keeps echoing it is running although it is not. I realized that the grep itself comes as a result and that is the issue.
如何跳过grep而仅检查过程?
How can I skip the grep and just check for the process?
推荐答案
使用 pgrep
:
if pgrep "$SERVICE" >/dev/null 2>&1 ; then
echo "$SERVICE is running"
fi
if pgrep -f "/path/to/$SERVICE" >/dev/null 2>&1 ; then
echo "$SERVICE is running"
fi
这篇关于使用Bash检查进程是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!