I have a Java application that I want to shutdown gracefully in Windows. I am starting this application using a .bat file and I am able to stop it gracefully if I press CTRL+C from keyboard. Sending a SIGINT/SIGTERM signal to an application is easy in Linux. I want to do something similar in Windows.
Till now I have tried with a batch file, but I can only try taskkill command in that which terminates the application abruptly.
I tried writing a python script with the following commands:
GenerateConsoleCtrlEvent -
GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, <my_process_id>)
The problem with this approach is that it needs process group id instead of process id. I think it will be same as the process id. But it fails saying that the parameter is incorrect.
os.kill -
os.kill(pid, signal.CTRL_C_EVENT)
This command abruptly kills my process probably because my process is not a subprocess of python.
I tried doing this with VBScript using terminate command given in the following link- How to terminate process using VBScript
But no luck. I do not want to use a third party app like SendKeys for this.
Now I am trying to write a program which can-
- Get a list of threads which are running in the java process created by my application.
- Get the name of the thread that I want to pass as a parameter to removeShutdownHook(thread_name)
Remove the shutdown hook by calling
Runtime.getRuntime().removeShutdownHook(<my_thread_name>);
I tried getting the thread details as mentioned in the following link Get a List of all Threads currently running in Java
but it gives me the threads which are running in my current process.
I am struggling with this problem from last 2 days. Is there any way to remove shutdown hook of another process in java in windows?
Thanks in advance!!
Update 1 - I have to achieve is using Java 6.
Update 2 - The answer given in the following link How to gracefully handle the SIGKILL signal in Java
will not work for me as it suggests adding a shutdown hook, that I already have in my application. I want to remove it from another application by getting the thread name.