代码之家  ›  专栏  ›  技术社区  ›  meallhour

如何查找进程是否正在运行?[关闭]

  •  -2
  • meallhour  · 技术社区  · 6 年前

    我已经创建了下面的bash脚本来查明进程是否正在运行

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

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

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

    2 回复  |  直到 6 年前
        1
  •  0
  •   Jafari MM    6 年前


    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
  •  0
  •   r2oro    6 年前

    你得到了 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