Skip to content

Instantly share code, notes, and snippets.

@hummus
Forked from somic/gist:123889
Last active December 30, 2015 21:39
Show Gist options
  • Save hummus/7889036 to your computer and use it in GitHub Desktop.
Save hummus/7889036 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Prints out how many seconds ago this EC2 instance was started
# It uses Last-Modified header returned by EC2 metadata web service, which as far
# as I know is not documented, and hence I assume there is no guarantee
# that Last-Modified will be always set correctly.
# if this fails for some non-ec2 instance, then
# try to stat a file that might be from os-install time
# Use at your own risk.
python <<EOP
from datetime import datetime;import urllib2;
try:
created_time = datetime.strptime(urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id',timeout=0.5).headers['last-modified'][5:-4], '%d %b %Y %H:%M:%S')
print (datetime.now() - created_time ).seconds
except:
import os,time
created_time = os.stat('/etc/ssh/ssh_host_rsa_key.pub').st_ctime
print int(time.time()-created_time)
EOP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment