Monday 23 November 2015

Always Run Crontab on Primary Node

How to ensure an shell script always runs on the primary node in an active / passive cluster only


We have two Oracle database servers in an active / passive cluster.I have a shell script which needs to be run by the oracle user and scheduled via crontab. However I only want this script to run on which ever node is the primary. I want to automate this task so I can leave both running crons active and concentrate on another tasks. When the nodes failover the new primary will continue to run the script, the new passive node will not. There is a virtual IP (VIP) address which is set up to always point to the primary node.

A sysadmin showed me this really cool trick which does this very task:

Example crontab entry

  • The inconfig command checks whether the virtual IP address is configured on the server (i.e the server is primary).
Note - change the above IP address (127.0.0.1)to match your VIP
  • If it is then the command returns true and the remainder of the statement is executed (i.e the my_script.sh is executed).
  • If the first part of the statement returns false then it exits and the script is not run.

This cron entry can then be added on both sides safe in the knowledge that it will only execute on the primary. There are certainly other ways to perform this tasks such as using a heartbeat or pacemaker, but this is a quick, simple solution which appears to work well.

Hope this helps.

No comments:

Post a Comment