Failed to restart Jenkins on Windows using jenkins.war

Hi, I’m having an issue with restarting a Jenkins instance. Little background on my setup:

I’m running Jenkins 2.387.1 on Windows via this command in cmd using java version 17.0.4.1

java -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar C:\jenkins\jenkins.war --httpPort=9090

I have tried using jenkins.exe and adding it as a Windows service, but that didn’t work for me because the automation tests I’m running need to be able to use the windows GUI… but that’s not important now.

When I restart Jenkins using the URL localhost:9090/restart I get the following error in the cmd window:

2023-05-30 09:27:52.185+0000 [id=17635] INFO    hudson.lifecycle.Lifecycle#onStatusUpdate: Restart in 5 seconds
2023-05-30 09:27:57.186+0000 [id=17635] INFO    hudson.lifecycle.Lifecycle#onStop: Stopping Jenkins as requested by tpmteam
2023-05-30 09:27:57.210+0000 [id=17635] INFO    hudson.lifecycle.Lifecycle#onStatusUpdate: Stopping Jenkins
2023-05-30 09:27:57.327+0000 [id=17635] INFO    jenkins.model.Jenkins$16#onAttained: Started termination
2023-05-30 09:27:57.353+0000 [id=17635] INFO    h.p.b.global.Lifecycle#shutdown: Shutdown complete - Global TimeOut ScheduledExecutorService had 0 tasks pending
2023-05-30 09:27:57.363+0000 [id=17635] INFO    jenkins.model.Jenkins$16#onAttained: Completed termination
2023-05-30 09:27:57.363+0000 [id=17635] INFO    jenkins.model.Jenkins#_cleanUpDisconnectComputers: Starting node disconnection
2023-05-30 09:27:57.370+0000 [id=17635] INFO    j.s.DefaultJnlpSlaveReceiver#channelClosed: restart thread for TPMBuild terminated: java.nio.channels.ClosedChannelException
2023-05-30 09:27:57.459+0000 [id=17635] INFO    jenkins.model.Jenkins#_cleanUpShutdownPluginManager: Stopping plugin manager
2023-05-30 09:27:57.552+0000 [id=17635] INFO    jenkins.model.Jenkins#_cleanUpPersistQueue: Persisting build queue
2023-05-30 09:27:57.569+0000 [id=17635] INFO    jenkins.model.Jenkins#_cleanUpAwaitDisconnects: Waiting for node disconnection completion
2023-05-30 09:27:57.570+0000 [id=17635] INFO    hudson.lifecycle.Lifecycle#onStatusUpdate: Jenkins stopped
2023-05-30 09:27:57.597+0000 [id=17635] WARNING jenkins.model.Jenkins$19#run: Failed to restart Jenkins
java.io.IOException: CreateProcess error=2, The system cannot find the file specified
        at java.base/java.lang.ProcessImpl.create(Native Method)
        at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:494)
        at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:159)
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
Caused: java.io.IOException: Cannot run program "C:\jenkins\jenkins.exe" (in directory "C:\jenkins"): CreateProcess error=2, The system cannot find the file specified
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
        at hudson.Proc$LocalProc.<init>(Proc.java:254)
        at hudson.Proc$LocalProc.<init>(Proc.java:223)
        at hudson.Launcher$LocalLauncher.launch(Launcher.java:997)
        at hudson.Launcher$ProcStarter.start(Launcher.java:509)
        at hudson.Launcher$ProcStarter.join(Launcher.java:520)
        at hudson.lifecycle.WindowsServiceLifecycle.restart(WindowsServiceLifecycle.java:146)
        at jenkins.model.Jenkins$19.run(Jenkins.java:4552)

The question here is why the Jenkins instance (started via the .war file) is trying to call the jenkins.exe that doesn’t exist?
I tried creating a jenkins.exe file from a .bat containing the java line to start Jenkins but still get other issues.

I’d recommend running an agent on the system instead of running Jenkins as not-a-service. This would allow you to run your tests and also run Jenkins in the preferred way on Windows. The Windows life-cycle is expecting that there is a service running, this is why you get this error. The name of the class is WindowsServiceLifecycle as you can see in the stacktrace, and this is what you specified in the command line to run. If you want to use a different lifecycle, you can pass that on the command line.

Yes, I forgot to mention, I do run a Jenkins Agent on the system, and i use that Agent to run the automation tests:

java -jar C:\jenkins\jenkins-agent2\agent.jar -jnlpUrl http://localhost:9090/computer/User1/jenkins-agent.jnlp -secret THE_SECRET_HERE -workDir "C:\jenkins\jenkins-agent2"

I have tried installing Jenkins as a service and then running the tests via an agent, but still no luck running tests because it doesn’t interact with the GUI of Windows (no idea why). Only this combination of running jenkins.war controller and then an agent.jnlp agent works and lets the automation tests interact with Windows GUI.

The issue I’m having is when I install updates for plugins, and I must restart the Jenkins controller to implement the updates.

Should I run the controller with a different -Dhudson.lifecycle? I see there are 4 different Lifecycle subclasses where WindowsServiceLifecycle resides. Is it one of these or something else? And is it ok if I run it with UnixLifecycle for example?

Did you assign the job to the agent that was running? You can do this using labels for the nodes. If you are starting the agent via the command line (instead of using a service) it should be able to interact with the GUI. Running things as a service disallows interaction with the GUI.

Running things as a service disallows interaction with the GUI.

Yes, I found this out the hard way. I am assigning the job to the agent using label for the node.

Besides, my automation tests are running ok now. Everything is running fine. My problem is restarting the Jenkins controller. When I restart i get the error described above.

My previous question is:

Should I run the controller with a different -Dhudson.lifecycle ? I see there are 4 different Lifecycle subclasses where WindowsServiceLifecycle resides. Is it one of these or something else? And is it ok if I run it with UnixLifecycle for example?

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 )