Skip to content

Instantly share code, notes, and snippets.

@wilvk
Created April 22, 2018 10:13
Show Gist options
  • Save wilvk/845739a9caa9973c1d40521a94986b10 to your computer and use it in GitHub Desktop.
Save wilvk/845739a9caa9973c1d40521a94986b10 to your computer and use it in GitHub Desktop.
Get the total number of seconds that the ec2-user has been idle - useful for shutting down instances
#!/bin/bash
RAW_TIME=$(w -hs ec2-user | awk '{print $4}')
SECONDS=0
MINUTES=0
TOTAL_SECONDS=0
DAYS=0
if [[ $RAW_TIME = *"days"* ]]; then
DAYS=$(echo $RAW_TIME | sed -e 's/days//g')
TOTAL_SECONDS=$(($DAYS * 86400))
elif [[ $RAW_TIME = *"s"* ]]; then
SECONDS=$(echo $RAW_TIME | sed -e 's/s//g')
TOTAL_SECONDS=$SECONDS
elif [[ $RAW_TIME = *"m"* ]]; then
TEMP_MINS=$(echo $RAW_TIME | sed -e 's/m//g')
MINUTES=$(echo $TEMP_MINS | cut -d ':' -f 1)
SECONDS=$(echo $TEMP_MINS | cut -d ':' -f 2)
TEMP_SECONDS=$(($MINUTES * 60))
TOTAL_SECONDS=$(($TEMP_SECONDS + $SECONDS))
fi
echo "Seconds: $SECONDS"
echo "Minutes: $MINUTES"
echo "Days: $DAYS"
echo "Total Seconds: $TOTAL_SECONDS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment