0

How to automatically remove an instance from its LoadBalance (ELB) when you stop the instance?

I've tried to create a shutdown script and put it on chkconfig, but it is not working:

#!/bin/bash
# loggly authorization 
#
# chkconfig: 2345 20 10
# description: add host logs to loggly
# Source function library.
. /etc/init.d/functions
. /etc/profile.d/aws-apitools-common.sh

LB=MyLB
OK="\033[60G\e[0;32mOK\e[0m"
EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`"
export JAVA_HOME=/usr/java/default

start() {
    echo "started service"
}

stop() {
        echo "removing $EC2_INSTANCE_ID from Load Balancer"
        elb-deregister-instances-from-lb $LB --instances=$EC2_INSTANCE_ID -I $AWS_ACCESS_KEY_ID -S $AWS_SECRET_ACCESS_KEY --quiet
        sleep 20
}


restart() {
        stop
        sleep 5
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?
3
  • Try specifying the path to wget, elb-deregister-instances-from-lb, etc., as it's probably running with a minimal default $PATH.
    – ceejayoz
    Jan 22, 2013 at 17:57
  • nothing... I did a simple command: echo "test" >/test.txt and the file test.txt does not exists after I start the server again... Jan 22, 2013 at 19:17
  • That could be the same thing. As your normal user, do which echo, then use that explicit path to test. It's probably something like /bin/echo.
    – ceejayoz
    Jan 22, 2013 at 19:31

1 Answer 1

0

There must be a /var/lock/subsys/my_app file

Put a touch /var/lock/subsys/my_app in the startup function and a rm /var/lock/subsys/my_app in the stop function

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .