s3cmd not running in AWS shutdown script

Jim Davis
2014-02-06
2014-02-06
  • Jim Davis
    Jim Davis
    2014-02-06

    I want to use s3cmd to preserve the logfiles on an Amazon Linux instance when the instance terminates (mainly as part of an auto-scaling group, when the group scales down). I have a script that runs fine from the command line. It also executes on reboot/shutdown (it logs its message in the /var/log/messages file), but the critical part, the s3cmd command, isn't executing when the script is run as part of the shutdown or reboot process. Is there some reason why s3cmd wouldn't run when executed as part of the shutdown process? The script containing the s3 command is linked to K000sendlogtos3 in the rc0.d and rc6.d directories, so it should get executed before any of the services are stopped. Any ideas?

     
  • Jim Davis
    Jim Davis
    2014-02-06

    I figured it out (with some help). It was a pathing problem -- not only for the s3cmd tool, but also for the ec2-metadata tool. And I needed to explicitly state the location of the s3cmd config file. Here is the working script, in case anyone is interested:

    !/bin/bash

    PATH=$PATH:/usr/sbin:/opt/aws/bin

    start(){
    touch /var/lock/subsys/0sendlogtos3
    }

    stop() {

    S3_BUCKET=<<bucket_name>>
    EC2_INSTANCE_STRING="ec2-metadata -i"
    LOG_FILE_LOCATION=/var/log/httpd/

    s3_dest=s3://$S3_BUCKET/${EC2_INSTANCE_STRING:13}/
    s3cmd --config /root/.s3cfg put -r $LOG_FILE_LOCATION $s3_dest >> /var/log/messages 2>&1
    wait
    echo "date" - sendlogtos3 executed >> /var/log/messages
    rm -f /var/lock/subsys/0sendlogtos3
    }

    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    esac

    ##END