Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Last active August 29, 2015 14:10
Show Gist options
  • Save ngyuki/61bf0262dc1bf450499a to your computer and use it in GitHub Desktop.
Save ngyuki/61bf0262dc1bf450499a to your computer and use it in GitHub Desktop.
run script from user data on aws for centos

Install

curl -o /etc/init.d/auto-userscript \
  https://gist.githubusercontent.com/ngyuki/61bf0262dc1bf450499a/raw/auto-userscript

chmod +x /etc/init.d/auto-userscript

chkconfig --add auto-userscript

License

MIT

#! /bin/bash
#
# chkconfig: 2345 50 50
. /etc/init.d/functions
prog=auto-userscript
# default: EC2 user-data
URL=http://169.254.169.254/latest/user-data
if [ -f /etc/sysconfig/$prog ]; then
. /etc/sysconfig/$prog
fi
function start() {
echo -n "Start auto run userscript"
(
set -eu
local tmp=
exec 1> >(logger -t $prog -i)
exec 2>&1
function fin () {
[ -f "$tmp" ] && rm -fv "$tmp"
}
trap fin EXIT
tmp=$(mktemp --suffix=.$prog)
echo "download $URL -> $tmp"
curl -fsS "${URL}" -o "$tmp"
if head -1 "$tmp" | grep '^#!' >/dev/null; then
chmod +x "$tmp"
echo ">>> run userscript"
"$tmp"
echo "<<< end userscript"
fi
)
rc=$?
[ $rc -eq 0 ] && success || failure
echo
return $rc
}
case "$1" in
start)
start
exit $?
;;
stop|status)
exit 0
;;
*)
echo $"Usage: $0 {start|stop|status}"
exit 2
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment