bash script to check running process

Posted by elasticsecurity on Stack Overflow See other posts from Stack Overflow or by elasticsecurity
Published on 2010-05-25T09:17:58Z Indexed on 2010/05/25 9:21 UTC
Read the original article Hit count: 396

Filed under:

I wrote a bash-script to check if a process is running. It doesn't work since the ps command always returns exit code 1. When I run the ps command from the command-line, the $? is correctly set, but within the script it is always 1. Any idea?

#!/bin/bash
SERVICE=$1

ps -a | grep -v grep | grep $1 > /dev/null
result=$?
echo "exit code: ${result}"
if [ "${result}" -eq "0" ] ; then
    echo "`date`: $SERVICE service running, everything is fine"
else
    echo "`date`: $SERVICE is not running"
fi

Bash version: GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)

© Stack Overflow or respective owner

Related posts about bash