I've created an LSB compatible init script which I intend to use to push some log files to an Amazon S3 bucket when the system is going down but it doesn't get executed.
Here is the script contents in in file /etc/init.d/push-apache-logs-to-s3.sh (I replaced with my client's branding name with XXX):
#! /bin/sh
### BEGIN INIT INFO
# Provides: push-apache-logs-to-s3
# Required-Start:
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Push Apache log files to S3
# Description:
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/lsb/init-functions
do_stop () {
LOG_DIR=/var/log/apache2/
BACKUP_DEST=s3://XXX-backups/api-logs
EC2_ID=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`
echo -n "Entering log directory..."
if cd $LOG_DIR
then
echo "OK"
else
echo
echo "Cannot change directory... BACKUP ABORTED!"
echo
exit 3
fi
echo -n "Pushing log files to S3..."
# push log files
if s3cmd put XXX-api_access.log* $BACKUP_DEST/$EC2_ID/ >/dev/null
then
echo "OK"
else
echo
echo "LOG FILES COULD NOT BE MOVED TO S3! BAKCUP NOT COMPLETE!"
echo
exit 5
fi
echo "*** Backup finished at " `date`
echo "-----------------------------------------------"
exit 0
}
case "$1" in
start)
# No-op
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
stop)
do_stop
;;
*)
echo "Usage: $0 start|stop" >&2
exit 3
;;
esac
:
Here is the command I used to install it:
sudo update-rc.d push-apache-logs-to-s3.sh start 01 2 3 4 5 . stop 01 0 1 6 .
which provided output:
Adding system startup for /etc/init.d/push-apache-logs-to-s3.sh ...
/etc/rc0.d/K01push-apache-logs-to-s3.sh -> ../init.d/push-apache-logs-to-s3.sh
/etc/rc1.d/K01push-apache-logs-to-s3.sh -> ../init.d/push-apache-logs-to-s3.sh
/etc/rc6.d/K01push-apache-logs-to-s3.sh -> ../init.d/push-apache-logs-to-s3.sh
/etc/rc2.d/S01push-apache-logs-to-s3.sh -> ../init.d/push-apache-logs-to-s3.sh
/etc/rc3.d/S01push-apache-logs-to-s3.sh -> ../init.d/push-apache-logs-to-s3.sh
/etc/rc4.d/S01push-apache-logs-to-s3.sh -> ../init.d/push-apache-logs-to-s3.sh
/etc/rc5.d/S01push-apache-logs-to-s3.sh -> ../init.d/push-apache-logs-to-s3.sh
The script uses s3cmd which is installed and configured on the system. s3cmd is in /usr/bin/ which is defined as PATH in the script.
When I execute the script by sudo ./push-apache-logs-to-s3.sh stop, it does the job.
Any suggestions what's wrong?