Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 589
  • Last Modified:

Runtime.getRuntime().exec() Processes FREEZE after 60 secs

I have a link on a jsp page to start a perl script to ping servers.  The perl script looks at a table in the mysql db and gets the ip addresses.
Everything works fine if I start the perl script from a shell manually - it runs until i stop it.  But, if i use my link on the web page, the process ALWAYS freezes after 60 secs.  never 61, never less than 60, always 60.
The code for the jsp page that starts the perl script is:
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="inms" uri="/WEB-INF/Tags.tld" %>
<jsp:include page="header.jsp"/>
Process procb = null;
String txt = null;
   Process proca = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c","ps -e | grep inmscheck.pl | grep -v grep | awk {'print $1'}"});
   BufferedReader input = new BufferedReader(new InputStreamReader(proca.getInputStream()));
   if ((txt=input.readLine()) != null) {
      %><inms:error msg="Daemon already running with PID:"/>
      <B><% out.println(txt);%></b><br>
      <a href="login2.jsp"><b>OK</b></a><%
   } else {
      procb = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c","/opt/tomcat/webapps/inms/jsp/inmscheck.pl"});
      %><inms:ok msg="Daemon started"/>
      <a href="login2.jsp"><b>OK</b></a><%
<jsp:include page="footer.jsp"/>
proca checks to see if the process is running before continuing - procb starts the perl script.  the <inms> tags are just my tags that put that messages in a nice table.
any ideas why it stops after 60 seconds when run like this?  is it something to do with linux and the way im starting the script?  is it automatically stopped after 60 secs?
I couldnt find anything on the net about this problem so I assume its something im doing wrong
1 Solution
if your script generates output, it will hang when the pipe is filled-up, because you never read it from the other end.
you should use procb.getInputStream(), and read all output until it finish or redirect output to /dev/null or a log file, e.g.:

{"/bin/sh","-c","/opt/tomcat/webapps/inms/jsp/inmscheck.pl >/dev/null 2>&1"});
psynnottAuthor Commented:
Perfect.  Thankyou :)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now