- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- running script in background thru Runtime.exec() h...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2004 08:03 PM
тАО10-12-2004 08:03 PM
running script in background thru Runtime.exec() hangs
I tried to ran a script in Linux (which has a background process) through the Java code using Runtime.exec(), it gets hanged. The same script is working fine when i ran it directly.
Is it a linux issue ? or any workaround is there ?
Any help would be highly appreciated.
thanks in advance
shiva.t
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2004 11:55 PM
тАО10-12-2004 11:55 PM
Re: running script in background thru Runtime.exec() hangs
I tried to write a short Java class on my Linux:
class Test
{
public static void main (String [] arg)
{
if (arg.length < 1)
System.exit (0);
try
{
Process p = Runtime.getRuntime().exec (arg[0]);
p.waitFor ();
} catch (Exception e)
{
e.printStackTrace ();
}
}
}
I run a script which puts another script in background. It works, except that I get no output on my terminal.
Linux is RedHat 8 and Java is Suns 1.4.1_01.
Bojan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2004 01:05 AM
тАО10-13-2004 01:05 AM
Re: running script in background thru Runtime.exec() hangs
thanks for your immediate response.
For your code pass the process to the stream, now you will get the output.
----
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((str = in.readLine()) != null) {
System.out.println(">>"+str);
}
}catch (FileNotFoundException fnfe) {
}catch (IOException fnfe) {}
-------
In my case, i am calling the script "startservs" which internally executes a java
code in background, which is used to start the server.
--
Process p=Runtime.getRuntime().exec(startservs);
--
Here it couldn't complete its process. So, it gets hanged. My aim is to complete the
process with the output.
thanks
shiva.t
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2004 04:24 AM
тАО10-13-2004 04:24 AM
Re: running script in background thru Runtime.exec() hangs
I misunderstood the java.lang.Process documentation (read to fast ;). With yours code pass the program runs ok and with output. I tested also with running another java code and it also work ok.
Maybe yours script or java code needs some input? If you look at the documentation http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Process.html
there is a pass:
"Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock."
Bojan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 05:27 PM
тАО10-19-2004 05:27 PM
Re: running script in background thru Runtime.exec() hangs
i have the same problem.
have you found a solution for this? please share.
saleem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 05:44 PM
тАО10-19-2004 05:44 PM
Re: running script in background thru Runtime.exec() hangs
Can you share the script which had the background process?
I think the issue comes when the background process is a continuous one.
Thanks,
Saleem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 07:37 PM
тАО10-19-2004 07:37 PM
Re: running script in background thru Runtime.exec() hangs
I did this only for test. Substitute my test directory /home/bojan/tmp with yours. The first is a one line script (testa):
/home/bojan/tmp/testb &
The second script (testb):
echo `date` >> /home/bojan/tmp/test.log
ls /home/bojan/tmp/
pwd
java SubTest 1 2 3 4
echo `date` >> /home/bojan/tmp/test.log
Now the two Java classes, the first (rewised with Shivas suggestions):
class Test
{
public static void main (String [] arg)
{
if (arg.length < 1) System.exit (0);
try
{
Process p = Runtime.getRuntime().exec (arg[0]);
BufferedReader in = new BufferedReader (new InputStreamReader(p.getInputStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(">>"+str);
}
p.waitFor ();
System.out.println ("Exit");
} catch (Exception e)
{
e.printStackTrace ();
}
}
And a dummy SubTest:
class SubTest
{
SubTest (String [] args)
{
try
{
for (int i = 0; i < args.length; i++) {
System.out.println ("arg " + i + " = " + args[i]);
Thread.currentThread().sleep(Long.parseLong (args[i]) * 1000);
}
} catch (Exception e)
{
e.printStackTrace ();
}
}
public static void main (String [] args)
{
new SubTest (args);
}
}
You run all with:
java Test /home/bojan/tmp/testa
Attached is this text, to save java indents.
Bojan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 09:17 PM
тАО10-19-2004 09:17 PM
Re: running script in background thru Runtime.exec() hangs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 11:06 PM
тАО10-19-2004 11:06 PM
Re: running script in background thru Runtime.exec() hangs
If I understand well, this is the same as if you modify my Test class in:
class Test
{
public static void main (String [] arg)
{
if (arg.length < 1) System.exit (0);
try
{
Process p = Runtime.getRuntime().exec (arg[0]);
} catch (Exception e)
{
e.printStackTrace ();
}
}
?
I try, it exit immediately after creating the process, the background script lives and do its job. The same is if I ommit testa and run directly testb or SubTest.
Bojan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-20-2004 02:48 AM
тАО10-20-2004 02:48 AM
Re: running script in background thru Runtime.exec() hangs
Thanks for your frequent responses.
In your code, the loop will execute based on your arguments length. Anyway the process would complete with delay.
Whereas in our case the server will going to run continuously. So, i assume that checking with infinite loop in background will be useful
Any ideas will be hightly appreciated.
Thanks
Shiva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-20-2004 04:06 AM
тАО10-20-2004 04:06 AM
Re: running script in background thru Runtime.exec() hangs
Ok, I try with a modified SubTest which is in an infinite loop:
class SubTest
{
SubTest (String [] args)
{
try
{
for (;;) {
System.out.println ("arg = " + args[0]);
Thread.currentThread().sleep(Long.parseLong (args[0]) * 1000);
}
} catch (Exception e)
{
e.printStackTrace ();
}
}
public static void main (String [] args)
{
new SubTest (args);
}
The results are identical to the previous test. The process remains active until my kill command. I did all 3 tests (testa,testb and SubTest without any shell script)
Bojan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-20-2004 07:14 AM
тАО10-20-2004 07:14 AM
Re: running script in background thru Runtime.exec() hangs
Linux - RedHat 9
Java -
java version "1.3.1"
jdkgcj 0.2.3 (http://www.arklinux.org/projects/jdkgcj)
gcj (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
and
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)
I add two lines to the testb script before running the java SubTest:
read x
echo $x >> /home/bojan/tmp/test.log
In Test.java I read one line from the process output stream and write one line to the input stream with:
OutputStreamWriter os = new OutputStreamWriter (p.getOutputStream ());
os.write ("test\n" , 0 , 5);
os.flush ();
and exit from program.
This works ok. Then I delete the line which does flush. With gcj all ok, but with Sun java the process die.
Bojan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-20-2004 06:42 PM
тАО10-20-2004 06:42 PM
Re: running script in background thru Runtime.exec() hangs
It seems that if I do not access the streams, the process continues to work. The problem with regard to the hanging is solved. But the pitfall here is that I cannot get the output of the program.
The hanging issue comes into picture when I try to access the InputStream of the process. For now, I have created a workaround. I call my main script from another script, which has mainscript >> mainscript.log, and then from the java file I empty the contents of mainscript.log to the console. So job done! Your point about the OutputStream was really helpful. Thanks a bunch!
Now, I want to see the reason why the programs hangs when we try to access the InputStream? Any ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-20-2004 06:56 PM
тАО10-20-2004 06:56 PM
Re: running script in background thru Runtime.exec() hangs
Try to ask Google "java process stream hang"
http://www.google.com/search?hl=en&lr=&safe=off&q=java+process+stream+hang
There are many descriptions of this simptom. Maybe some of them will explain yours questions.
Bojan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-20-2004 08:18 PM
тАО10-20-2004 08:18 PM
Re: running script in background thru Runtime.exec() hangs
My considerations abbout standard I/O streams (not tested!).
I think that, if you have a background process, which will live alone after the main process will exit, you must reassign all three streams to something known (probably files) with System.setErr(), System.setIn() and System.setOut(). If you do not do that, the streams are cuted out when the main process exits and writing or reading will produce some strange effects (hangs, exceptions etc). For the communication between the main process and the background process you must use some other method (maybe sockets) or implement some sort of "protocol" which will tell to both sides, when the communication is finished. When the communication is finished the background process will reassign the streams and the main process will not try to write or read to the streams. Probably you can close them (but this must be tested).
Another time: This are my considerations which was not tested. When you get in such troubles, it is a good practice to write some small programs to test the behavior (and with Java that should be done on different OSes).
Bojan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-28-2004 10:54 PM
тАО10-28-2004 10:54 PM
Re: running script in background thru Runtime.exec() hangs
I have followed your thread with interest as I am having difficulties in similar areas.
Context:
- I am writing an application that mimicks a high availability solution so that I can run a configurable number of processes on a configurable number of linux servers. On each box resides a java process (Daemon) that starts the processes for me based on input from a remote server (uses RMI).
Problem:
- unable to stop an external process
Symptoms:
- if I run a c++ executable directly (for example a simple compiled exe that just does some looping) and I call destroy on the process, the exe dies as I would expect
- if I create a very simple shell script and run it and then try and kil it all is well .. eg
#!/bin/csh -f
while (1)
echo "hello"
end
- if I then try to run a java process in a ksh script and try and kill it, the destroy method does not actually kill the process at all .. a really nasty one if you are trying to abstract the OS/host from the user. Have you come accross this? Have you tried to run other java processes with the java.lang.Process class?