Finally found a solution, or rather a workaround.
I’m starting my Jenkins instance using a batch file, run by Task Scheduler.
Previously I was just running the java command, as the last line in the batch file, using the
-Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle
property and that’s what was causing the error on restart.
After some investigation into WindowsServiceLifecycle, inspired by Alex Earl, I found out that I can also use hudson.lifecycle.ExitLifecycle
. Now this property value is closing the Jenkins controller instead of restarting it, but it gives a ErrorLevel code 5. Because of this I was able to modify my batch script into the following:
:START_JENKINS
java -Dhudson.lifecycle=hudson.lifecycle.ExitLifecycle -jar C:\jenkins\jenkins.war --httpPort=9090
if %ERRORLEVEL% EQU 5 (
GOTO :START_JENKINS
) else ( GOTO :eof )