Skip to content

Instantly share code, notes, and snippets.

@nazoking
Created September 15, 2016 04:07
Show Gist options
  • Select an option

  • Save nazoking/af30a2182cdd5d6731d987d3c1a4286f to your computer and use it in GitHub Desktop.

Select an option

Save nazoking/af30a2182cdd5d6731d987d3c1a4286f to your computer and use it in GitHub Desktop.
spot インスタンスが止められそうなら stop ファイルを作る
#!/bin/bash
# スポットインスタンスの停止イベントをチェックして stop ファイルを書き出す
set -e
BASE_DIR=$(dirname $0)
STOP_FILE=${STOP_FILE-$BASE_DIR/stop}
PID_FILE=${PID_FILE-$BASE_DIR/check_stop.pid}
if [ "$1" == "stop" ];then
shift
if [ -e $PID_FILE ];then
if ps $(<$PID_FILE) >/dev/null ;then
kill $PID_FILE
echo "already exists $(<$PID_FILE)"
exit
fi
fi
echo "no running $(<$PID_FILE)"
exit
fi
if [ -e $PID_FILE ];then
if ps $(<$PID_FILE) >/dev/null ;then
echo "already exists $(<$PID_FILE)"
exit
else
rm $PID_FILE
fi
fi
echo "start $0"
echo $$ > $PID_FILE
if [ -e $STOP_FILE ];then
rm $STOP_FILE
fi
while :
do
if curl -s http://169.254.169.254/latest/meta-data/spot/termination-time | grep -q .*T.*Z; then
echo terminated > $STOP_FILE
break
fi
sleep 5
done
echo "done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment