473,372 Members | 928 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

process.destroy() does not kill the subprocess

127 100+
Dear All,

I have an issue with destroy() method of java.lang.Process class. All what I am trying to do is, controlling the execution of one program through another. Let's say, Program B has to be executed conditionally through Program A based on the commands it gets from the user. Let's say, we have two inputs, "start" and "stop" to drive the Program B.

I have thought of various alternatives.

(1) Using Thread to control the execution of Program B. But then stopping a process means via a thread is not allowed as the related methods are deprecated (stop/suspend etc.,)
(2) Using ThreadGroup ( to have only one thread as its member) and calling 'destroy' on the group. But it again falls on the same track as every thread should be stopped before the destroy operation is attempeted.
(3) Process/ProcessBuilder via Runtime -- seems to be the better way to obtain a process reference and call destroy(), waitFor() etc., to control the spawned child process.


I went ahead with 3rd approach and all went fine till the spawning of the new child process. When the time came to stop the execution (ideally speaking, to stop! but practically destroying/killing the child process), it breaks! .

My main program (Program A) gives an information that the Program B stopped successfully (as I do store the process references in a Map and retrieve the same before destroying the process) and this operation succeeds. But it does NOT really reflect in reality and my child program still runs as if it was never interrupted. Looks like the child process gets disjoint with the parent process and hence the link gets cut between the two. I have to invoke my Program B via "cmd.exe /C java.exe ProgramB". I do use Windows XP and JRE 1.6.0_10 beta.

Program B is a java program which just keeps writing the date and time into a log file on an interval of 1 second. Program A has the main() method and gets the "threadName && start/stop" inputs from the user and retains the information into the Map. I am not dependent on my Program B's output and hence I don't invoke waitFor() and exitValue() methods.

If required I can post the program here.

Having been searching in the internet for a long time but nothing seems to be fruitful. Some says that it may be a bug with JVM. But I don't think so.

Few typical examples of this kind is, Tomcat catalina (startup.bat/shutdown.bat) and Quartz Job Scheduler -- they do get a command from the user 'start/stop' to fire an action!

Looks strange as the API does NOT guarantee the behavior as per the Specification. It says,

Expand|Select|Wrap|Line Numbers
  1. public abstract void destroy()  
  2.          Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.   

Do I miss something here? Any suggestions would be of a great help!
Jun 9 '09 #1
6 16873
r035198x
13,262 8TB
Going back to your first alternative, just because the stop method is deprecated doesn't mean you can't stop a thread from executing. You can still use a flag to control a while loop that runs in the thread's run method. All you do is switch the flag when the stopping condition is reached. Once the run method ends, the thread will stop.
Jun 9 '09 #2
itsraghz
127 100+
Hi r035198x,

Hope you are doing fine. Thanks for the reply!

Yes of course. You are right. Just because a method is deprecated does not mean that we can't/should not use. However, this usage is strictly NOT suggested by Sun itself as they are potentially dangerous (wrt the locks the objects might have acquired) Reference URL -> http://java.sun.com/j2se/1.5.0/docs/...precation.html.

I was about to have the other alternative you suggested. Like having a logical lock to control the execution. Let me test the other possibilities and get back.

Thanks.
Jun 9 '09 #3
r035198x
13,262 8TB
Read my reply again. I didn't say that you should use the stop method, I pointed out how to stop a thread without using the stop method.
Jun 9 '09 #4
itsraghz
127 100+
Yes, that is what I also meant. Apologize if it was not reflected in the words.
Jun 9 '09 #5
r035198x
13,262 8TB
I see no need for pursuing all these other alternatives. You can stop a thread using the approach I posted above. It's also described in that link you posted above and suggested as best practice.
Jun 9 '09 #6
itsraghz
127 100+
yes of course. but I felt proceeding with the process itself as I can get hold of the entire process so as to destroy/kill at once completely! Thus, have chosen this way.
Jun 9 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Natan | last post by:
Hi. I have a python script under linux where I poll many hundreds of interfaces with mrtg every 5 minutes. Today I create some threads and use os.system(command) to run the process, but some of...
1
by: Manfred Braun | last post by:
Hi All, I am writing a tool, which should monitor some exe-processes, which are not very solid. Th main function is to re-start them, if they hung, but this is complicated. I can detect things...
3
by: elrondrules | last post by:
Hi Am new to python and need your help!! this is my code snip. within my python script I have the following commands.. <snip> import os
4
by: Richard Rossel | last post by:
Hi Fellows, I have a problem with process termination. I have a python code that apache runs through a django interface. The code is very simple, first, it creates a process with the...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
2
by: John Nagle | last post by:
There doesn't seem to be any way to portably kill another process in Python. "os.kill" is Mac/Unix only. The "signal" module only lets you send signals to the current process. And the...
1
by: Salim Fadhley | last post by:
Does anybody know of a python module which can do process management on Windows? The sort of thing that we might usually do with taskmgr.exe or process explorer? For example: * Kill a process...
2
by: =?Utf-8?B?WVhR?= | last post by:
Hello, In the parent process(A), run update function, and start the update process(B), in B process, kill A, then download and update files, then restart A. I want to know can the B process kill...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.