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
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 |