Bash script didn't recognize command result

63 views Asked by At

i have a bash script to check the status of our RAID System.

#!/bin/bash
[email protected]

zpool status -x | grep 'all pools are healthy'

if [ $? -ne 0 ]; then
    /bin/date > /tmp/zfs.stat
    echo >> /tmp/zfs.stat
    /bin/hostname >> /tmp/zfs.stat
    echo >> /tmp/zfs.stat
    /sbin/zpool status -x >> /tmp/zfs.stat
    cat /tmp/zfs.stat | /usr/bin/mail -s "Disk failure in server : `hostname`" $EMAIL_ADD
fi

When i run the script manually it works so i decided to run this script as a cronjob. But the cronjobs sends the mail every time. I really hope you can help me to fix this.

1

There are 1 answers

0
amin khozaei On

Testing of exit status with $? is not a good practice, for more infromation go to testing exit value. You should replace your if condition with the COMMAND(zpool ...),as follow:

if ! zpool status -x | grep 'all pools are healthy';then
...
fi