Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to restart Jenkins service programmatically on a schedule

I'd like to be able to set up a scheduled job that restarts Jenkins every night (Windows7). Is there a way to set up a job to run that will do a safeRestart programmatically that I can put in my Windows scheduler?

like image 510
Andy G Avatar asked Jan 17 '14 18:01

Andy G


Video Answer


2 Answers

If the above CLI method did not work (in my case it failed on: hudson.security.AccessDeniedException2: anonymous is missing the Overall/Read permission)

You can create a Groovy plugin build step and "Execute a system Groovy script":

import hudson.model.*;
Hudson.instance.doSafeRestart(null);

or the new Jenkins instance

import jenkins.model.*
Jenkins.instance.doSafeRestart(null);

Then, you can set this job to be triggered by schedule. For example, to restart Jenkins daily on midnight, set "Build periodically": H 00 * * *

like image 136
Noam Manos Avatar answered Oct 11 '22 09:10

Noam Manos


Posting this here for others that come here from Google. You can add a job to Jenkins to restart itself using the CLI. Add a job with a shell step to execute:

java -jar "$JENKINS_HOME/war/WEB-INF/jenkins-cli.jar" -s "$JENKINS_URL" safe-restart
like image 39
Ivan Neeson Avatar answered Oct 11 '22 10:10

Ivan Neeson