0

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:

  1. 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.

  2. 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-

  1. Get a list of threads which are running in the java process created by my application.
  2. Get the name of the thread that I want to pass as a parameter to removeShutdownHook(thread_name)
  3. 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.

Community
  • 1
  • 1
blu3
  • 123
  • 1
  • 12
  • @jim-garrison Thanks for providing the link but the answer given there is for Linux. Also, as stated above, I already have shutdown hook in my application. I want to remove it using another program outside my application. – blu3 Nov 18 '16 at 07:23
  • You can't reach into a Java process from outside like that unless you build in an IPC mechanism into the target program (the one you want to control from outside). You will need to launch a thread listening on a socket or pipe and allow the external program to connect and send an appropriate message, which is then acted upon. – Jim Garrison Nov 18 '16 at 07:26
  • @JimGarrison Will exposing it as an MBean on a jmx port help? – blu3 Nov 18 '16 at 07:29
  • That could be one way, yes. – Jim Garrison Nov 18 '16 at 07:29
  • @JimGarrison So as I understand now, there is no way to get the threads run by another java process using a java program. Also, I don't think it is possible by any scripting language as well. Please suggest if my understanding is correct. – blu3 Nov 18 '16 at 07:32
  • Not using standard Java. I have no idea if it's possible using JNI but I suspect not without root-level (admin in Windows terms) access. Even so, tweaking a process' threads from the outside would be a serious security violation and make all sorts of malware possible. Either write your own control mechanism or use JMX (which is almost the same thing). – Jim Garrison Nov 18 '16 at 07:35
  • @Jim: All 'remote' JMX access within a machine requires the same userid, and between machines can be configured with various JAAS and SSL options. +blu3: The default/builtin ThreadMXBean, accessible either local or remote, allows you to get quite a bit of information, but not including shutdown hook, and doesn't allow you to change anything. So yes you need your own mbean. However, I don't see how removing a shutdown hook assists in your stated goal of doing 'shutdown gracefully'. – dave_thompson_085 Nov 18 '16 at 11:06

0 Answers0