#!/usr/bin/env bash # shellcheck disable=SC2015 # vim:ts=4:sts=4:sw=4:et # # Author: Hari Sekhon # Date: 2019-11-07 14:25:06 +0000 (Thu, 07 Nov 2019) # # https://github.com/harisekhon/bash-tools # # License: see accompanying Hari Sekhon LICENSE file # # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback # # https://www.linkedin.com/in/harisekhon # # https://aws.amazon.com/blogs/aws/new-ec2-spot-instance-termination-notices/ set -euo pipefail [ -n "${DEBUG:-}" ] && set -x # intentionally not using /lib so that this script is standalone and easier to distribute to VMs rather than requiring a full git clone of the repo usage(){ cat </dev/null; then echo "This script must be run from within an EC2 instance as that is the only place the AWS EC2 Metadata API is available" exit 2 fi termination_time="" while true; do # regex borrowed from AWS Systems Administration book by O'Reilly and also here: # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-interruptions.html termination_time="$(curl -s --max-time 1 http://169.254.169.254/latest/meta-data/spot/termination-time | grep '.*T.*Z' || :)" if [ -n "$termination_time" ]; then break fi echo -n '.' # AWS recommended check interval sleep 5 done echo "Termination Time: $termination_time" echo "Executing: $*" # eval'ing so that "command1; command2" works as well as 'x=test; echo $x' eval "$@"