Skip to content

Instantly share code, notes, and snippets.

@peritus
Created May 9, 2011 16:31
Show Gist options
  • Save peritus/962835 to your computer and use it in GitHub Desktop.
Save peritus/962835 to your computer and use it in GitHub Desktop.
Shuts down idle jenkins instances
#
# This script shuts down my AWS EC2 hosted Jenkins if
# there were no builds in the last hour.
#
# Set this script to run as build step in jenkins every minute like this
#
# [x] Build periodically
#
# Schedule * * * * *
#
# There will be only one build job at a time that is constantly restarted if
# there is activity, and shuts down if there is none.
#
# To allow the user running the jenkins server to shut down the instance,
# fire up `visudo` and add this line:
#
# jenkins ALL=NOPASSWD: /sbin/shutdown
#
RUN_FREQUENCY=3600 # in seconds
sleep $(($RUN_FREQUENCY - 100))
LAST_BUILD=`curl --silent http://localhost:8080/rssAll|tr '>' '\n'|grep /updated|head -n 1|tr 'TZ</updated' ' '`
test $((`date -u +%s` - `date -u -d"$LAST_BUILD" +%s`)) -lt $(($RUN_FREQUENCY - 100)) || sudo shutdown -h +1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment