Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to kill a child process spawned by Java when tomcat is shutdown
    primarykey
    data
    text
    <p>I have written a service for JIRA(a web application runs in tomcat) which runs periodically(say 1 hour). Basically, the service executes a system command thru <code>runtime.exec(command)</code> and parses the output generated by the command then updates a Lucene index with it, output will be huge.</p> <p><strong>The problems are:</strong></p> <p>1) If I shutdown tomcat with <code>shutdown.sh</code> while the above service is executing, the java(or the catalina) process is not getting killed. Both the java &amp; child process are living for a while i.e., until the system command completes &amp; service processes the output. But then the service fails to update the index leaving the index in an inconsistent state.</p> <p>If I shutdown tomcat when the above service is not running, everything is good. I think, <a href="http://java-monitor.com/forum/showthread.php?t=553&amp;goto=nextoldest" rel="nofollow noreferrer">this is explained here</a>. I am still not clear why JVM won't shutdown as the above service is running within tomcat? </p> <p>Note that this is the only java app running on that machine.</p> <p>2) Then, if I kill java using <code>kill &lt;pid&gt;</code>, both the java &amp; child process are getting killed contradicting to <a href="https://stackoverflow.com/a/295748/181293">this post</a>. </p> <p>Is this because the child process is sending output to parent(java) and once parent is killed, the child has no idea where to send the output and thus got killed ?</p> <p>3) I tried to use <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29" rel="nofollow noreferrer">shutdownhook</a> as explained <a href="https://stackoverflow.com/a/261133/181293">in this post</a>, but that's not working for me. The code inside the <code>shutdownhook</code> is getting executed only after the java &amp; child processes are done with their work. So, calling <code>process.destroy()</code> inside shutdownhook is not useful here.</p> <p>This seems obvious, as the JVM is still running in my case, it won't call shutdownhooks until it starts it's shutdown sequence. Don't know how this worked for the other guy, I mean, how come the child process spawned by java is still running when JVM is down.</p> <p>4) If I restart tomcat, new java process with different pid is generated.</p> <p>Is it possible to stop the child process programmatically when tomcat is shutdown ?</p> <p>Let me know if I am not clear with my explanation...</p> <p>Here is the code that executes system command:</p> <pre><code> String command = getCommand(); File view = new File(viewPath); Runtime runtime = Runtime.getRuntime(); try { final Process process = runtime.exec(command, null, view); StreamReader errorStreamReader = new StreamReader(process .getErrorStream()); Thread errorStreamThread = new Thread(errorStreamReader); errorStreamThread.start(); revisions = parseRevisionLogs(process.getInputStream()); process.waitFor(); process.getInputStream().close(); process.getErrorStream().close(); process.getOutputStream().close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload