【问题标题】:How to find if process is running or not? [closed]如何查找进程是否正在运行? [关闭]
【发布时间】:2018-12-08 05:59:38
【问题描述】:
我创建了以下 bash 脚本来确定进程是否正在运行
ps -ef | grep process_name
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running."
fi
但是,脚本总是返回"Process is running."
请提出正确的方法来确定进程是否正在运行。
【问题讨论】:
标签:
linux
process
grep
rhel
ps
【解决方案1】:
PR=$(ps -ef | grep process_name | wc -l)
if [ "$PR" -ge 2 ]; then
echo "Process is running."
else
echo "Process is not running."
fi
第一行,总是有包含grep process_name 的输出。所以运行过程在第二行。
【解决方案2】:
您的进程列表中会出现grep process_name。所以请确保它被省略:)
ps -ef | grep -v "grep process_name" | grep process_name
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running."
fi