#by leal

#2012-04-13

#!/bin/bash

[ $(id -u) -ne 0 ] && echo "Must run as root" && exit
pidfile="/var/run/keepalived.pid"
conf="/etc/keepalived.conf"


do_start(){
    [ -e $pidfile ] && echo "Already running..." && exit
    echo "Keepalived starting..."
    keepalived -f $conf --pid $pidfile
    sleep 3
    [ -e $pidfile ] && echo "Keepalived start success" || echo "Failed"
}

do_stop(){
   if [ -e $pidfile ];then
        pid=$(cat $pidfile)
   else
    echo "Not runnint..."
    exit
   fi
   echo "stoping keepalived..."
   kill $pid
   sleep 2
   [ ! -e $pidfile ] && echo "Stop success" || echo "Stop failed!!!"
}

case $1 in
    "start")
        do_start
    ;;
    "stop")
    do_stop
    ;;
    "restart")
    if [ -e $pidfile ];then
        do_stop
        do_start
    else
        do_start
    fi
    ;;
    *)
    echo "Usage: $(basename $0) start|stop|restart"
    ;;
esac