如何查找进程是否为 运行?

How to find if process is running or not?

我创建了以下 bash 脚本来查明进程是否 运行

ps -ef | grep process_name 
if [ $? -eq 0 ]; then
  echo "Process is running."
else
  echo "Process is not running."
fi

但是,脚本总是返回 "Process is running."

请建议正确的方法来确定进程是否 运行。


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 自身的输出。所以 运行 进程出现在第二行。

您的进程列表中 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