2012/03/25

Restarter script for jenkins

Restarter script for Jenkins

  • I am fighting with my Jenkins instance being down every other day.
  • Getting bored of sshing to the host and restart the instance, I wrote a small restarter script.
#!/usr/bin/python
"""
Simple restarter for jenkins - not a real Tanuki wrapper :-).
- Install to /usr/local/bin and make the script executable.
- Then create a cron entry to execute the script every 10 minutes:
cat << EOF > /etc/cron.d/restartjenkins
*/10 * * * * root /usr/local/bin/restartjenkins.py 2>&1 | logger -t restartjenkins
EOF
"""
import os
import sys
import urllib2
SERVICE_TO_RESTART = "/etc/init.d/jenkins restart"
URL_TO_CHECK = os.getenv("URL_TO_CHECK", "http://127.0.0.1:8000/hudson/")
def restart():
os.system(SERVICE_TO_RESTART)
if __name__ == "__main__":
try:
urllib2.urlopen(URL_TO_CHECK)
open("/tmp/%s.timestamp" % os.path.basename(sys.argv[0]), "w").write(URL_TO_CHECK)
except urllib2.URLError, e:
error = sys.exc_info()
error_message = "%s\n%s" % (error[0], error[1])
sys.stderr.write(error_message)
restart()

1 comment:

  1. Do you have a specific reason for not using JSW?

    ReplyDelete