Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

# loggly authorization 
# chkconfig: 2345 20 10
# description: add host logs to loggly
# Source function library.
. /etc/init.d/functions
. /etc/profile.d/

EC2_INSTANCE_ID="`wget -q -O - || 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() {
        sleep 5

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

exit $?
share|improve this question
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 '13 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... – Daniel Cukier Jan 22 '13 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 '13 at 19:31
I found the solution, it is down here :-) thanks – Daniel Cukier Jan 22 '13 at 21:25

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.