#!/bin/bash # # Activate/deactivate script for crontabs # # Author: Johan De Meersman # Description: A script to failover cronjob activity, for those days when you # really need that extra bit of certainty. # Syntax: Crontab (start|stop|restart|status) # Config: Not really :-) See man(8) crontab for cronfile syntax. # Support: Your brain, Google, Your local guru, me, Linux-HA mailing list, http://linux-ha.org/contact/ - in that order # License: GPL # Revision: 1.0 - 20050421 - Initial version, should do most tricks you want. # Source function library. . /etc/ha.d/shellfuncs # This is to stabilize crontab behaviour in at least some versions of Debian: # The "out-of-the-box" behaviour for crontab -l is to display the # three line "DO NOT EDIT THIS FILE" header that is placed at the # beginning of the crontab when it is installed. The problem is that it # makes the sequence # # crontab -l | crontab - # # non-idempotent -- you keep adding copies of the header. This causes pain # to scripts that use sed to edit a crontab. Therefore, the default # behaviour of the -l option has been changed to not output such header. # You may obtain the original behaviour by setting the environment # variable CRONTAB_NOHEADER to 'N', which will cause the crontab -l # command to emit the extraneous header. CRONTAB_NOHEADER=Y # We don't want no stinkin' headers # Parameter grabbing WHO_ME=$(basename $0) CRON_USER=$1 CRON_ACTIVE_FILE=$2 CRON_INACTIVE_FILE=$3 COMMAND=$4 # Main functions service_start () { ha_log "info: $WHO_ME: activating service" if [ "$CRON_ACTIVE_FILE" == "-r" -a "$(crontab -u $CRON_USER -l 2>/dev/null)" == "" ]; then # If you try to delete a crontab which has already been deleted, cron # returns with an error status. Hence this special case where we don't # even try. ha_log "info: $WHO_ME: activated" else if crontab -u $CRON_USER $CRON_ACTIVE_FILE 2>/dev/null ; then ha_log "info: $WHO_ME: activated" else ha_log "ERROR: $WHO_ME: Service did not activate correctly!" exit 1 fi fi } service_stop () { ha_log "info: $WHO_ME: deactivating service" if [ "$CRON_INACTIVE_FILE" == "-r" -a "$(crontab -u $CRON_USER -l 2>/dev/null)" == "" ]; then # If you try to delete a crontab which has already been deleted, cron # returns with an error status. Hence this special case where we don't # even try. ha_log "info: $WHO_ME: deactivated" else if crontab -u $CRON_USER $CRON_INACTIVE_FILE 2>/dev/null; then ha_log "info: $WHO_ME: deactivated" else ha_log "ERROR: $WHO_ME: Service did not deactivate correctly!" exit 1 fi fi } service_status () { # For simplicity's sake, we assume that if the running crontab matches one # of the config files, the service is in that state - regardless of what # you last asked it to do. RUNNING_CRON=$(crontab -u $CRON_USER -l 2>/dev/null) if [ "$CRON_ACTIVE_FILE" == "-r" ]; then EXPECTED_ACTIVE_CRON="" else EXPECTED_ACTIVE_CRON=$(cat $CRON_ACTIVE_FILE) fi if [ "$CRON_INACTIVE_FILE" == "-r" ]; then EXPECTED_INACTIVE_CRON="" else EXPECTED_INACTIVE_CRON=$(cat $CRON_INACTIVE_FILE) fi case "$RUNNING_CRON" in "$EXPECTED_ACTIVE_CRON") ha_log "info: $WHO_ME: Service is active" ;; "$EXPECTED_INACTIVE_CRON") ha_log "info: $WHO_ME: Service is inactive" ;; *) ha_log "WARNING! $WHO_ME: Running configuration does not match either configuration file! Dumping into /tmp" DUMP_DATE=$(hadate) echo $RUNNING_CRON > /tmp/heartbeat.$WHO_ME.unexpected.running echo $EXPECTED_ACTIVE_CRON > /tmp/heartbeat.$WHO_ME.expected.active echo $EXPECTED_INACTIVE_CRON > /tmp/heartbeat.$WHO_ME.expected.inactive exit 1 esac } script_usage () { ha_log "Usage: $WHO_ME (start|stop|restart|status)" } # Grab the command(s) we need find_command () { if ! which $1 >/dev/null 2>&1; then ha_log "ERROR: $WHO_ME: Can't find command '$1'" exit 1 fi } # The first one's here to make sure we have what we need to find the other # commands. No touchy-touchy. Strong bad touchy-touchy! find_command which find_command crontab find_command getent # Verify some parameters if [ "$CRON_ACTIVE_FILE" != "-r" -a ! -r "$CRON_ACTIVE_FILE" ]; then ha_log "ERROR: can not read datafile $CRON_ACTIVE_FILE" script_usage exit 1 fi if [ "$CRON_INACTIVE_FILE" != "-r" -a ! -r "$CRON_INACTIVE_FILE" ]; then ha_log "ERROR: can not read datafile $CRON_INACTIVE_FILE" script_usage exit 1 fi if ! getent passwd $CRON_USER > /dev/null; then ha_log "ERROR: user $CRON_USER does not exist" script_usage exit 1 fi # Showtime ! case "$COMMAND" in start) service_start ;; stop) service_stop ;; restart) service_stop service_start ;; status) service_status ;; *) script_usage exit 1 esac exit 0