4

I am trying to check if a process is running with the code below:

SERVICE="./yowsup/yowsup-cli"
RESULT=`ps aux | grep $SERVICE`

if [ "${RESULT:-null}" = null ]; then
    echo "not running"
else
    echo "running"
fi

But it keeps echoing it is running although it is not. I realized that the grep itself comes as a result and that is the issue.

How can I skip the grep and just check for the process?

7 Answers7

13

Use pgrep:

if pgrep "$SERVICE" >/dev/null 2>&1 ; then
    echo "$SERVICE is running"
fi

or, more reliable:

if pgrep -f "/path/to/$SERVICE" >/dev/null 2>&1 ; then
    echo "$SERVICE is running"
fi
hek2mgl
  • 143,113
  • 25
  • 227
  • 253
7

For systems where pgrep isn't available you can use:

service="[.]/yowsup/yowsup-cli"

if ps aux | grep -q "$service"; then
    echo "not running"
else
    echo "running"
fi
  • [.] in will force grep to not list itself as it won't match [.] regex.
  • grep -q can be utilized to avoid command substitution step.
  • Prefer using lowercase variables in shell.
anubhava
  • 713,503
  • 59
  • 514
  • 593
4

The problem is that grep you call sometimes finds himself in a ps list, so it is good only when you check it interactively:

$ ps -ef | grep bash
...
myaut    19193  2332  0 17:28 pts/11   00:00:00 /bin/bash
myaut    19853 15963  0 19:10 pts/6    00:00:00 grep --color=auto bash

Easiest way to get it is to use pidof. It accepts both full path and executable name:

service="./yowsup/yowsup-cli" # or service="yowsup-cli"
if pidof "$service" >/dev/null; then
    echo "not running"
else
    echo "running"
fi

There is more powerful version of pidof -- pgrep.


However, if you start your program from a script, you may save it's PID to a file:

service="./yowsup/yowsup-cli"
pidfile="./yowsup/yowsup-cli.pid"
service &
pid=$!
echo $pid > $pidfile

And then check it with pgrep:

if pgrep -F "$pidfile" >/dev/null; then
    echo "not running"
else
    echo "running"
fi

This is common technique in /etc/init.d start scripts.

myaut
  • 10,658
  • 2
  • 26
  • 59
1

I thought pidof was made for this.

function isrunning()
{
    pidof -s "$1" > /dev/null 2>&1
    status=$?
    if [[ "$status" -eq 0 ]]; then
        echo 1
    else
        echo 0
    fi
)
if [[ $(isrunning bash) -eq 1 ]]; then echo "bash is running"; fi
if [[ $(isrunning foo) -eq 1 ]]; then echo "foo is running"; fi
John Schmitt
  • 1,082
  • 17
  • 37
1
## bash

## function to check if a process is alive and running:

_isRunning() {
    ps -o comm= -C "$1" 2>/dev/null | grep -x "$1" >/dev/null 2>&1
}

## example 1: checking if "gedit" is running

if _isRunning gedit; then
    echo "gedit is running"
else
    echo "gedit is not running"
fi

## example 2: start lxpanel if it is not there

if ! _isRunning lxpanel; then
    lxpanel &
fi

## or

_isRunning lxpanel || (lxpanel &)

Note: pgrep -x lxpanel or pidof lxpanel still reports that lxpanel is running even when it is defunct (zombie); so to get alive-and-running process, we need to use ps and grep

Bach Lien
  • 850
  • 5
  • 6
0
current_pid="$$" # get current pid
# Looking for current pid. Don't save lines either grep or current_pid
isRunning=$(ps -fea | grep -i $current_pid | grep -v -e grep -e $current_pid)

# Check if this script is running
if [[ -n "$isRunning" ]]; then
    echo "This script is already running."
fi
Cristian
  • 500
  • 6
  • 7
0
SERVICE="./yowsup/yowsup-cli"
RESULT=`ps aux | grep $SERVICE|grep -v grep`

if [ "${RESULT:-null}" = null ]; then
    echo "not running"
else
    echo "running"
fi
Jumbo
  • 1
  • 1
  • 1
    Please share more details such that others can learn from it – Nico Haase Feb 19 '21 at 13:32
  • The community encourages adding explanations alongisde code, rather than purely code-based answers (see [here](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers)). – costaparas Feb 20 '21 at 00:11