source: trunk/locker/cron/bin/heartbeat @ 1285

Last change on this file since 1285 was 359, checked in by quentin, 17 years ago
initial checkin of cron system
File size: 1.8 KB
Line 
1#!/bin/bash
2
3CRONROOT=/afs/athena.mit.edu/contrib/scripts/cron
4
5# Find our real hostname
6
7# This big long mess just results in a list of ip/name.
8for i in `/sbin/ip addr show dev eth0 | grep ' inet ' | cut -f 6 -d ' ' | cut -f 1 -d '/' | xargs -n 1 host | cut -f 1,5 -d ' ' | sed 'y/ /\//'`; do
9        hostip=`echo $i | cut -f 1 -d '.'`
10        name=`echo $i | cut -f 2 -d '/'`
11        case $name in
12                SCRIPTS*) echo "$name";;
13                *) echo "Heartbeat for $name ($hostip)"; HOSTNAME=$name; HOSTIP=$hostip;;
14        esac;
15done
16
17# Tell everyone who's watching that we're alive
18touch $CRONROOT/servers/$HOSTNAME
19
20# Sleep based on our IP, in an attempt to not collide with another server also trying to gain control of the mirroring
21sleep $(($HOSTIP - 50))
22
23# Find the current master
24MASTER="DOES-NOT-EXIST"
25current_server () {
26        for i in $CRONROOT/server-crontabs/*; do
27                if [ -h $i ]; then
28                        MASTER=`basename $i`
29                        echo "Current master $MASTER"
30                fi
31        done
32}
33if lockfile -1 -r10 -l90 $CRONROOT/lock/heartbeat.lock; then
34    current_server
35
36        # The only way to compare times in bash is to compare the modtimes of two files.
37        compare=`mktemp /tmp/heartbeat-compare.XXXXXXXXXX`
38        touch -d '2 minutes ago' $compare
39       
40        if [[ $CRONROOT/servers/$MASTER -ot $compare ]]; then
41                # Master died! Take over.
42                echo "Master '$MASTER' died! Taking over."
43                for i in $CRONROOT/server-crontabs/*; do
44                        if [ -h $i ]; then
45                                echo rm $i
46                                rm $i
47                        else
48                                echo rmdir $i
49                                rmdir $i
50                        fi
51                done
52                for i in $CRONROOT/servers/*; do
53                        server=`basename $i`
54                        case $server in
55                                $HOSTNAME)
56                                        echo ln -s ../crontabs/ $CRONROOT/server-crontabs/$HOSTNAME
57                                        ln -s ../crontabs/ $CRONROOT/server-crontabs/$HOSTNAME;;
58                                *)
59                                        echo mkdir $CRONROOT/server-crontabs/$server
60                                        mkdir $CRONROOT/server-crontabs/$server;;
61                        esac;
62                done;
63        fi
64
65        rm $compare
66    rm -f $CRONROOT/lock/heartbeat.lock
67fi
Note: See TracBrowser for help on using the repository browser.