I am trying to run springboot jar as window service and i was able to do using below script. It generates two process prunsrv.exe and java.exe. But on stopping the service it only stops prunsrv.exe(using //SS//Myservice) but i can see java.exe process still running which i have to end using task manager only. How to stop both the process at a time such that i dont have to search processin taskmanager to kill?
set "CLASSPATH=D:\temp-1.0.0.jar"
prunsrv.exe //IS//%SERVICE_NAME% ^
--Description "Myservice" ^
--DisplayName "%DISPLAYNAME%" ^
--Install "%EXECUTABLE%" ^
--LogPath "D:\MyService" ^
--Startup auto ^
--StdOutput auto ^
--StdError auto ^
--Classpath "%CLASSPATH%" ^
--Jvm "%JVM%" ^
--StartImage "%JAVA_HOME%\bin\java.exe" ^
--StartMethod start ^
--StopMethod stop ^
--StartMode exe ^
--StopMode java ^
--StartPath "D:\MyService" ^
--StopPath "D:\MyService" ^
--StartClass com.darth.MyService ^
--StopClass com.darth.MyService ^
--StartParams -jar#%CLASSPATH% ^
--StopParams stop ^
--JvmMs 1024 ^
--JvmMx 6144
Using Procrun in Java
mode almost certainly will not work:
When using the Java or exe modes, the Procrun service application (prunsrv) launches the target application in a separate process. The "stop" application needs to communicate somehow with the "start" application to tell it to stop. For example, using RPC.
(source Procrun documentation).
The easiest way to stop your service is to use the jvm mode, in which case Procrun starts the JVM itself and can call an arbitrary static method with parameters String[]
inside the same JVM to shut it down. E.g. your can modify your application like this:
@SpringBootApplication
public class MyService extends SpringBootServletInitializer {
public static void main(String[] args) {
String command = "start";
if (args.length > 0) {
command = args[args.length - 1];
}
switch (command) {
case "start":
SpringApplication.run(Application.class, args);
break;
case "stop":
System.exit(0);
break;
default:
}
}
}
and install it like this:
prunsrv.exe //IS//MyService ^
--Description "MyService" ^
--Startup auto ^
--Classpath "D:\temp-1.0.0.jar" ^
--StartMode jvm ^
--StartClass org.springframework.boot.loader.JarLauncher ^
--StopMode jvm ^
--StopClass org.springframework.boot.loader.JarLauncher ^
--StopParams stop ^
--LogPath "D:\MyService" ^
--LogLevel Debug ^
--StdOutput auto ^
--StdError auto
An undocumented feature of Procrun (cf. source code) allows to call System.exit(0)
directly without any modification to the application. You just need to set the stop class to java.lang.System
:
prunsrv.exe //IS//MyService ^
--Description "MyService" ^
--Startup auto ^
--Classpath "D:\temp-1.0.0.jar" ^
--StartMode jvm ^
--StartClass org.springframework.boot.loader.JarLauncher ^
--StopMode jvm ^
--StopClass java.lang.System ^
--LogPath "D:\MyService" ^
--LogLevel Debug ^
--StdOutput auto ^
--StdError auto