Process.destroy() terminates parent process in Linux environment

In my java stub program, I am trying to execute a shell program through Runtime.getRuntime().exec(). On forcible termination of the child process (i.e. killing shell program by callling Process.destroy()), it also terminates the parent process i.e. the java main program also. This same program runs fine in HP-Unix without terminating parent process, but fails in Linux. I am using JVM 1.4.2 version.

Also, I tried to execute perl program instead of shell script. With perl program, it works fine without terminating parent process.

Can anyone please help?

My java program .....VMUtest.java

import java.io.BufferedInputStream;

import com.ge.is.venus.eventhandling.EventHandler;

import com.ge.is.venus.eventhandling.VEPriority;

public class VMUtest {

/**

* @param args

*/

public static void main(String[] args) {

VMUtest test = new VMUtest();

boolean flag = false;

while(true){

System.out.println("inside while");

if(!flag){

test.testExec();

System.out.println("Started the script.... flag is set true");

flag = true;

}

}

}

public void testExec(){

// TODO Auto-generated method stub

Runtime rt = Runtime.getRuntime();

Process proc = null;

TimerThread timer = null;

long lTimeOut = 0l;

boolean timeOutFlag = true;

StringBuffer errorStrBuf = new StringBuffer();

StringBuffer resultStrBuf = new StringBuffer();

BufferedInputStream resultBufStr = null;

BufferedInputStream errorBufStr = null;

String fcmd="./shtest";

//String fcmd="C:/"

String timeOut="10";

try {

//System.out.println("Time out check block");

lTimeOut = Long.parseLong(timeOut.trim());

EventHandler.log(VEPriority.NOTICE , "Script execution time out:"+lTimeOut);

//System.out.println("--wh4hwkjfwTime out:"+lTimeOut);

//If timeout is 0 it means the script shud not have a timeout

if (lTimeOut == 0) {

//System.out.println("--NO time out");

EventHandler.log(VEPriority.NOTICE , "No time out for this script");

timeOutFlag = false;

}

} catch (Exception e) {

e.printStackTrace();

timeOutFlag = false;

//EventHandler.log(VEPriority.ERROR , Utilities.getStackTraceAsString(e));

}

try{

System.out.println("Start Executing");

proc = rt.exec(fcmd);

System.out.println("Done Executing");

if (timeOutFlag) {

System.out.println("Timer starting..." );

timer = new TimerThread(proc,1);

timer.start();

}

resultBufStr = new BufferedInputStream(proc.getInputStream());

byte[] resultByteArr = new byte[1024];

int resultLength=0;

while((resultLength = resultBufStr.read(resultByteArr,0,resultByteArr.length)) != -1) {

resultStrBuf.append(new String(resultByteArr, 0, resultLength));

System.out.println("message:"+resultStrBuf.toString());

}

errorBufStr = new BufferedInputStream(proc.getErrorStream());

byte[] errorByteArr = new byte[1024];

int errorLength=0;

//errorStrBuf.append(new String("-ERROR FROM THE SCRIPT"));

while((errorLength = errorBufStr.read(errorByteArr,0,errorByteArr.length)) != -1) {

//System.out.println("-");

errorStrBuf.append(new String(errorByteArr, 0, errorLength));

System.out.println("message1:"+errorStrBuf.toString());

}

System.out.println("timeOutFlag : " + timeOutFlag);

}catch (Exception e) {

System.out.println("Timer interrupted..." );

//e.printStackTrace();

timer = null;

//To make sure the process could be forked

if (proc != null) {

try{

if (proc.waitFor() != 0) {

EventHandler.log(VEPriority.ERROR , "Script execution forcibly ended because of timeout");

System.out.print("--Script Execution forcibly terminated because of time out--");

System.out.print("-Script Execution forcibly terminated because of time out--");

}

else {

System.out.print("Could not execute the script:"+fcmd);

}

}catch(InterruptedException ie){

ie.printStackTrace();

}finally{

}

}

}

}

}

Java Program TimerThread.java....

package edi2k.admin.ias;

import com.ge.is.venus.eventhandling.EventHandler;

import com.ge.is.venus.eventhandling.VEPriority;

//import edi2k.webtool.util.Utilities;

public class TimerThread extends Thread {

public static String icsVersion = "$Rev: 184 $";

public static String icsDLM = "$Date: 2006-12-08 00:01:26 +0530 (Fri, 08 Dec 2006) $";

public static String icsAuthor = "$Author: ubuild $";

private volatile boolean stopTimerThread = false;

private Process process;

private long timeOut = 0;

public TimerThread(Process process , long timeOut) {

this.timeOut = timeOut;

this.process = process;

}

public void run() {

long timeBeforeExecution = System.currentTimeMillis();

try {

while(!stopTimerThread) {

if (System.currentTimeMillis() - timeBeforeExecution < timeOut) {

long tempTimeOut = 0;

if (timeOut != 0) {

tempTimeOut = (long) (timeOut/5l);

} else {

//Default sleep time of 2 minutes

tempTimeOut = 120000l;

}

Thread.sleep(tempTimeOut);

continue;

}

stopTimerThread = true;

process.destroy();

}

if (stopTimerThread) {

//this.interrupt();

}

}

catch(Exception e) {

//EventHandler.log(VEPriority.ERROR , Utilities.getStackTraceAsString(e));

}

}

}

[5711 byte] By [locherla_sudhaa] at [2007-11-27 3:14:06]
# 1
1. Use code tags when posting code.2. Extract the problem code into a completely separate runnable java app (not fragment of generated code) which demonstrates the problem.
jschella at 2007-7-12 8:16:36 > top of Java-index,Java HotSpot Virtual Machine,Specifications...