-2

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.

meallhour
  • 13,921
  • 21
  • 60
  • 117
  • 2
    [How to determine whether a process is running or not and make use it to make a conditional shell script?](https://askubuntu.com/q/157779), [Check if a process is running using Bash](https://stackoverflow.com/q/29260576/608639), [Bash script to check running process](https://stackoverflow.com/q/2903354/608639), [Linux Script to check if process is running and act on the result](https://stackoverflow.com/q/20162678/608639), etc. – jww Dec 08 '18 at 06:47
  • 2
    Possible duplicate of [Check if a process is running using Bash](https://stackoverflow.com/questions/29260576/check-if-a-process-is-running-using-bash) – Tsyvarev Dec 08 '18 at 10:44

2 Answers2

0


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.

Jafari MM
  • 57
  • 1
  • 7
0

You're getting grep process_name on your process list. So makes sure it is omitted :)

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
r2oro
  • 43
  • 6