linuxprocessgreprhel

How to find if process is running or not?


I have created the following bash script to find out if process is running or not

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

However, the script is always returning "Process is running."

Please suggest the correct way to find out if the process is running or not.


Solution


  • PR=$(ps -ef | grep process_name | wc -l) 
    if [ "$PR" -ge 2 ]; then
        echo "Process is running."
    else
        echo "Process is not running."
    fi
    

    first line, always has the output including the grep process_name its self. so running process comes at second line.