Ubuntu – How to determine whether a process is running or not and make use it to make a conditional shell script


How can I determine if a process is running or not and then have a bash script execute some stuff based on that condition?

For example:

  • if process abc is running, do this

  • if it is not running, do that.

Best Answer

A bash script to do something like that would look something like this:


# Check if gedit is running
# -x flag only match processes whose name (or command line if -f is
# specified) exactly match the pattern. 

if pgrep -x "gedit" > /dev/null
    echo "Running"
    echo "Stopped"

This script is just checking to see if the program "gedit" is running.

Or you can only check if the program is not running like this:

if ! pgrep -x "gedit" > /dev/null
    echo "Stopped"