bash check count processes

Simple bash command to check if processes are running:

if [ $(pgrep -f pmon) ]
then
    echo One or more databases running
else
    echo No databases running
fi

Syntax gets more complex if you want to check for specific counts of processes:

case $(pgrep -f tnslsnr | wc -l | awk '{print $1}') in
    0)
         echo No listeners running
    ;;
    1)
         echo One listener running
    ;;
    *)
         echo More than one listener running
    ;;
esac
August 20, 2013

Leave a Reply

Your email address will not be published. Required fields are marked *