Bash Script Fails Conditional Test

Home » CentOS » Bash Script Fails Conditional Test
CentOS 11 Comments

Hey all,

I wrote a very basic script to determine if cassandra db is running. I’m setting a variable called ‘pid’ to the output of a ps | grep like to grab the pid of the cassandra process.

#!/bin/bash pid=$(ps -ef | grep cassandra | grep -v grep | grep -i -v -e grep -e screen
-e s3fs|awk ‘{print $2}’)

if [[ -e $pid ]]
then
echo “Cassandra is running with pid: $pid”
else
echo “Cassandra is DOWN!!!”
fi

But for some reason the script doesn’t realize that the pid variable has been set, and fails the condition. It then reports that Cassnadra is DOWN!!!”.

[root@web1:~] #sh -x ./bin/check-cass.sh
++ ps -ef
++ grep -v grep
++ grep -i -v -e grep -e screen -e s3fs
++ awk ‘{print $2}’
++ grep cassandra
+ pid&979
+ [[ -e 26979 ]]
+ echo ‘Cassandra is DOWN!!!’
Cassandra is DOWN!!!

Can anybody tell me where I’m going wrong here? Because from what I can see, clearly the pid variable is being set so the script should be reporting that cassandra is up!

I’d appreciate any advice you may have.

Thanks, Tim

11 thoughts on - Bash Script Fails Conditional Test

  • Tim Dunphy a écrit :

    Hi,

    -e checks file existence. As you don’t have a file named 26979 in your
    pwd, test fails logically. If you want to know if variable is set, you can use -z $pid. You could
    also try -d /proc/$pid.

    HTH, Laurent.

  • That did it!!

    [root@web1:~] #./bin/check-cass.sh Cassandra is running with pid: 26979

    This is what the script looks like now:

    #!/bin/bash pid=$(ps -ef | grep cassandra | grep -v grep | grep -i -v -e grep -e screen
    -e s3fs|awk ‘{print $2}’)

    if [[ -n $pid ]]
    then
    echo “Cassandra is running with pid: $pid”
    else
    echo “Cassandra is DOWN!!!”
    fi

    Insert an extra line after #!/bin/bash

    Good tip! But I ran the script with sh +x . I guess that running it with sh
    +xv would do the same thing. But that is a useful tip to include the debug lines right in the script. I’ll have to remember that for next time!

    Thanks! :)

    Tim

  • You should use “bash -x” (“bash” and not “sh” because sh may not be bash everywhere; eg Ubuntu; “-x” and not “+x” because “-x” means “turn on debug”
    but “+x” means “turn _off_ debug”)

  • Once upon a time, Tim Dunphy said:

    You can probably replace that with a much cleaner pid=$(pidof cassandra).

  • Once upon a time, Stephen Harris said:

    Unless you have specific bashisms (which I don’t think the original did, and you should mostly avoid in scripts), sh -x will be fine.

  • It’s a matter of “consistency”. The script began #!/bin/bash and so a direct shell invocation should _also_ use the same command.

  • Good to know! I hadn’t heard of pidof before. However this is what I get when I run it:

    [root@web1:~] #pidof cassandra
    [root@web1:~] #

    Returns nothing. However:

    [root@web1:~] #pidof java
    27210 11418 10852

    Gives me a few pids.

  • Once upon a time, Tim Dunphy said:

    Try pid=$(pidof -x cassandra). Normal pidof calls look for programs with the given name, but scripts/interpreted programs show up with the interpreter first and then the program name. The -x options tells it to look for script-like things as well.

LEAVE A COMMENT