Skip to content

Instantly share code, notes, and snippets.

@mfriedenhagen
Created March 25, 2012 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfriedenhagen/2199521 to your computer and use it in GitHub Desktop.
Save mfriedenhagen/2199521 to your computer and use it in GitHub Desktop.
Sample restarter script for jenkins
#!/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()
@kirankashalkar
Copy link

Doesn't seem to work.
I see the following errors in syslog:
Jun 23 08:03:01 jenkinsslave-virtual-machine CRON[18779]: (root) CMD ( /usr/local/bin/rj.py 2>&1 | logger -t restartjenkins )
Jun 23 08:03:01 jenkinsslave-virtual-machine restartjenkins: <class 'urllib2.URLError'>
Jun 23 08:03:01 jenkinsslave-virtual-machine restartjenkins: <urlopen error [Errno 111] Connection refused>Rather than invoking init scripts through /etc/init.d, use the service(8)
Jun 23 08:03:01 jenkinsslave-virtual-machine restartjenkins: utility, e.g. service jenkins restart
Jun 23 08:03:01 jenkinsslave-virtual-machine restartjenkins: /etc/init.d/jenkins: 54: /etc/init.d/jenkins: initctl: not found
Jun 23 08:03:01 jenkinsslave-virtual-machine restartjenkins:
Jun 23 08:03:01 jenkinsslave-virtual-machine restartjenkins: Since the script you are attempting to invoke has been converted to an
Jun 23 08:03:01 jenkinsslave-virtual-machine restartjenkins: Upstart job, you may also use the stop(8) and then start(8) utilities,
Jun 23 08:03:01 jenkinsslave-virtual-machine restartjenkins: e.g. stop jenkins ; start jenkins. The restart(8) utility is also available.
Jun 23 08:03:01 jenkinsslave-virtual-machine restartjenkins: /etc/init.d/jenkins: 102: /etc/init.d/jenkins: start: not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment