- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - Linux
- >
- General
- >
- running script in background thru Runtime.exec() h...
-
- Forums
-
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
- HPE Blog, Austria, Germany & Switzerland
- Blog HPE, France
- HPE Blog, Italy
- HPE Blog, Japan
- HPE Blog, Middle East
- HPE Blog, Russia
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
-
Blogs
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Blog, Latin America
- HPE Blog, Middle East
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
-
Information
- Community
- Welcome
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Tips and Tricks
- Resources
- Announcements
- Email us
- Feedback
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Aruba Airheads Community
- Enterprise.nxt
- HPE Dev Community
- Cloud28+ Community
- Marketplace
-
Forums
-
Blogs
-
Information
-
English
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
Hewlett Packard Enterprise International
- Communities
- HPE Blogs and Forum
© Copyright 2021 Hewlett Packard Enterprise Development LP