If you have scripts setup in a RedHat Cluster environment, where there are active and passive nodes, and are wondering how to make scripts only execute on the active cluster node, this script is a working example. It needs to be installed and running on each cluster node. The only variable to change is “MYHOST” depending on which node the script is running on. This analyzes the output of /usr/sbin/clustat for active node status and service type.
Feel free to use this as a starting point for other RH cluster aware wrappers.
#!/bin/sh
#mysql_service monolith-node01p started
MYHOST="myserver.org" #replace this hostname with whatever the hostname is of the node this script goes on
HOST_STATE=`clustat | tail -n 1 | awk {'print $2'}`
SERV_STATE=`clustat | tail -n 1 | awk {'print $3'}`
DATE=`date +'%c'`
LOG="/var/log/service_cluster_cronexec.log"
echo "DATE: $DATE"
echo "HOST_STATE: $HOST_STATE"
echo "SERV_STATE: $SERV_STATE"
if [ "$HOST_STATE" = "$MYHOST" ]; then
if [ "$SERV_STATE" = "started" ]; then
echo "Running cronexec..."
/var/www/html/monolith/cron/readcron.sh /var/www/html/monolith/cron root
echo "$DATE - mysql_service $SERV_STATE on $HOST_STATE :: Running Monolith Cronexec" >> $LOG
else
/bin/rm -f /etc/cron.d/monolith
echo "$DATE - mysql_service $SERV_STATE on $HOST_STATE :: Service not started, removing /etc/cron.d/monolith" >> $LOG
/etc/init.d/crond restart
fi
else
/bin/rm -f /etc/cron.d/monolith
echo "$DATE - mysql_service $SERV_STATE on $HOST_STATE :: Not active node, removing /etc/cron.d/monolith" >> $LOG
/etc/init.d/crond restart
fi
echo "DONE"
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.