My native process does not run properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    My native process does not run properly

    i did run a process throgh java on a server ...........Sun solaris.

    how can trap the process information ....
    and if the process run for a long time then process terminates in the mean time!

    can anyone figure me out why it happens ............... ......
    thanks for ur help
    Last edited by Niheel; Feb 2 '07, 05:19 PM.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by dmjpro
    i did run a process throgh java on a server ...........Sun solaris.
    how can trap the process information ....
    and if the process run for a long time then process terminates in the mean time!!!!!!!!!!! !!!!!!!!!!!!!!! !!!
    can anyone figure me out why it happens ............... ......
    thanks for ur helpppppppppppp pp
    not sure what the exact problem is? did you start the process via Runtime.exec() method and you need to capture the output?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      thhhannnnnnkkkk kksssssssssss for help
      yes u are right ..............
      but i want to run a shell scripts on solaris which will backup the database dump .
      whenever the process takes long time then it stops automatically.. ..
      i don't want to use Process.waitFor () ...without using it i can trap the process information.
      how long the process runs in background i don't want it stopped and trap the process information whenever i want.........
      plz help me out after see my reply ....
      i am online ............
      thhhhhhhhhhaaaa aaaaaaaannnnnnn nnnnkkkkkkkkkkk sssssssssssss.. .......

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by dmjpro
        thhhannnnnnkkkk kksssssssssss for help
        yes u are right ..............
        but i want to run a shell scripts on solaris which will backup the database dump .
        whenever the process takes long time then it stops automatically.. ..
        i don't want to use Process.waitFor () ...without using it i can trap the process information.
        how long the process runs in background i don't want it stopped and trap the process information whenever i want.........
        plz help me out after see my reply ....
        i am online ............
        thhhhhhhhhhaaaa aaaaaaaannnnnnn nnnnkkkkkkkkkkk sssssssssssss.. .......
        have a look at this
        Code:
        // execute a child process using java exec command and get output
        
        import java.io.*;
        import java.lang.*;
        
        public class JavaExec {
        
        public static void main (String args[]){
          try {
             // get runtime environment and execute child process
             Runtime systemShell = Runtime.getRuntime();
             Process output = systemShell.exec("ls *.c");
             // open reader to get output from process
             BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
             String line = null;
             System.out.println("<OUTPUT/>");
              while((line = br.readLine()) != null ) 
                 { System.out.println(line);  }          // display process output
             System.out.println("</OUTPUT>");
             int exitVal = output.waitFor();             // get process exit value
             System.out.println("Process Exit Value : "+ exitVal);
             }
           catch (IOException ioe){ System.err.println(ioe); }
           catch (Throwable t) { t.printStackTrace();}
        }
        }
        it runs ls *.c and gets its output - the br.readLine() returns null when the child terminates

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          i used exactly that code...
          but when my child process takes 10 - 15 minutes or more then my code is not working properly and the process terminates before muture death...
          using waitFor for shot term process is right but for long term process i think is not right ....
          plz help me out.......
          thannnkssssss.. ............... ....

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by dmjpro
            i used exactly that code...
            but when my child process takes 10 - 15 minutes or more then my code is not working properly and the process terminates before muture death...
            using waitFor for shot term process is right but for long term process i think is not right ....
            plz help me out.......
            thannnkssssss.. ............... ....
            which process terminates early - the parent or the child?
            I have had the following programs running on my machine for over an hour

            Code:
            // execute a child process using java exec command and get output
            
            import java.io.*;
            import java.lang.*;
            
            public class PassInt {
            
            public static void main (String args[]){
              try {
                 int a=1;          // value to pass to MyProgram
                 // get runtime environment and execute child process
                 Runtime systemShell = Runtime.getRuntime();
                 Process output = systemShell.exec("java MyProgram " + a);
                 // open reader to get output from process
                 BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
                 String line = null;
                 System.out.println("<OUTPUT/>");
                  while((line = br.readLine()) != null ) 
                     { System.out.println(line);  }          // display process output
                 System.out.println("</OUTPUT>");
                 int exitVal = output.waitFor();             // get process exit value
                 System.out.println("Process Exit Value : "+ exitVal);
                 }
               catch (IOException ioe){ System.err.println(ioe); }
               catch (Throwable t) { t.printStackTrace();}
            }
            }
            the above program starts the following
            Code:
            //package shellcommand;
            import java.io.*;
            import java.lang.*;
            
            public class MyProgram {
            
            public static void main (String args[]) throws Exception{
            System.out.println("Parameter " + args[0]);
            for(int i=0;i<100;i++) 
              { Thread.sleep(60000); System.out.println("*" + i); }
            }
            }
            which outputs an * every minute

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              my code is something like this ............... ......
              Code:
              //MyThread.java
              package com.iitkgp.coalnetpim.util;
              
              import java.io.*;
              
              public class MyThread implements Runnable
              {
                private Thread t;
                private String command;
                public Process NATIVE_PROCESS = null;
                public InputStream final_output = null;
                public int exitVal = Integer.MIN_VALUE;
                public String error_string = null; 
                public MyThread(String command)
                {
                  this.command = command;
                  t = new Thread(this);
                  t.start();
                }
                public void run()
                {
                  try
                  {
                    NATIVE_PROCESS = Runtime.getRuntime().exec(command);
                    //Thread.sleep(5000); //Keep it slept for 5 seconds so that the browser can get the intial response
                    /*exitVal = NATIVE_PROCESS.waitFor();
                    if(exitVal == 0) final_output = NATIVE_PROCESS.getInputStream();  //Normal termination
                    else final_output = NATIVE_PROCESS.getErrorStream(); //Abnormal termination*/
                  }catch(Exception e)
                  {
                    error_string = e.fillInStackTrace().toString();      
                  }
                }
                public boolean isAlive()
                {
                  return t.isAlive();
                }  
              }
              Code:
              //ThreadTest.jsp
              
              <%@ page contentType="text/html;charset=WINDOWS-1252"%>
              
              <html>
                <head>
                  <title>Simple Thread Test</title>
                  <frameset cols = "80%,*">
                    <frame src = "ThreadTestMain.jsp">
                    <frame src = "ThreadTestHidden.jsp">
                  </frameset>
                </head>
              </html>
              
              //ThreadTestMain.jsp
              <%@ page contentType="text/html;charset=WINDOWS-1252"%>
              
              <%@ page import = "com.iitkgp.coalnetpim.util.*"%>
              
              <html>
                <head>    
                </head>
                <body>
                  <%
                  try{
                    String command = "sh /user6/oracle9iAS/coalnet_screen_backup/backup_from_screen 2 a0d1m4 mms";
                    MyThread my_thread = new MyThread(command);
                    session.setAttribute("my_thread",my_thread);
                  }catch(Exception e)
                  {
                    request.setAttribute("error_text",e.fillInStackTrace().toString());
                  %>
                    <jsp:forward page = "../../common/jsp/error_page.jsp" />
                  <%
                  }
                  %>
                  <center><h1>Process running started ............</h1></center>
                </body>
              </html>
              
              //ThreadTestHidden.jsp
              <%@ page contentType="text/html;charset=WINDOWS-1252"%>
              
              <%@ page import = "com.iitkgp.coalnetpim.util.*"%>
              <%@ page import = "java.io.*"%>
              
              <html>
                <head>
                  <script language = javascript>
                    function start_page()
                    {
                      
                      try{
                      if(document.all.error_occured)
                      {
                        window.parent.frames[0].document.body.innerHTML += "<br>" + document.all.error_occured.value;
                        window.clearInterval();
                        return;
                      }
                      if(document.all.thread_status.value.toUpperCase() == "FALSE")
                      {
                        /*if(document.all.exit_value.value == "0")
                          window.parent.frames[0].document.body.innerHTML = document.all.output.value;
                        else window.parent.frames[0].document.body.innerHTML += "<br><br>" + document.all.output.value;*/
                        window.parent.frames[0].document.body.innerHTML += "<br><br><br>Process terminated ...........";
                        window.clearInterval();
                      }
                      else
                      {
                        //window.parent.frames[0].document.body.innerHTML = document.all.output.value;
                        window.parent.frames[0].document.body.innerHTML += "<br>Process still running ...........";
                        window.setInterval(reload,2000);
                      }
                      }catch(err){
                        alert(err.description);
                        window.clearInterval();
                        }        
                    }
                    function reload()
                    {
                      window.location.reload();
                    }
                  </script>
                </head>
                <body onload = start_page()>
                <!--<body>-->
                <%
                  boolean thread_status = false;
                  MyThread my_thread = null;
                  try{
                    my_thread = (MyThread)session.getAttribute("my_thread");
                    //my_thread.NATIVE_PROCESS.destroy();
                    if(my_thread != null){
                    thread_status = my_thread.isAlive();
                    if(thread_status)
                    {
                      //BufferedReader output = new BufferedReader(new InputStreamReader(my_thread.NATIVE_PROCESS.getInputStream()));
                      StringBuffer string_output = new StringBuffer("");
                      //String line;
                      /*while((line = output.readLine()) != null)
                      {
                        string_output.append("<br>");
                        string_output.append(line);
                      }*/
                      //output.close();
                      %>
                        <input type = hidden name = output value = "<%=string_output.toString()%>">
                      <%
                    }
                    else
                    {
                      //BufferedReader output = new BufferedReader(new InputStreamReader(my_thread.final_output));
                      StringBuffer string_output = new StringBuffer("");
                      //String line;
                      /*while((line = output.readLine()) != null)
                      {
                        string_output.append("<br>");
                        string_output.append(line);
                      }*/
                      %>
                        <input type = hidden name = exit_value value = "<%=my_thread.exitVal%>">
                        <input type = hidden name = output value = "<%=string_output.toString()%>">
                      <%
                      //output.close();
                      session.removeAttribute("my_thread");
                    }
                   }
                   else{%>
                    <input type = hidden name = error_occured value = "An internal error occured">
                   <%}
                  }catch(Exception e)
                  {
                  %>
                    <input type = hidden name = error_occured value = "<%="Debasis" + e.fillInStackTrace().toString()%>">
                  <%
                  }
                  %>
                  <input type = hidden name = thread_status value = "<%=thread_status%>">
                  <%if(my_thread.error_string != null){%>
                    <input type = hidden name = error_occured value = "<%="Moumita" + my_thread.error_string%>">
                  <%}
                  %>
                  <input>
                </body>
              </html>
              At first the link called the TestThread.jsp. ..
              The comment line i first tried to execute but the expected output i didn't get......
              the ThreadTest.jsp starts the thread and will show the current process status
              the ThreadTestHidde n.jsp will looks after the process by sending request in every two minutes.
              and the command run on the solaris is ....
              sh /user6/oracle9iAS/coalnet_screen_ backup/backup_from_scr een 2 a0d1m4 mms
              2 is for module choice
              and mms for module name
              the module contains thousands of tables and other objects such as procedures....e tc...etc.......
              the scripts are ...........

              Code:
              //backup_from_screen
              choice=$1
              case $choice in
                      1) adm_password=$2
                         sh /user6/oracle9iAS/coalnet_screen_backup/all_backup_db $adm_password ;;
                      2) module_codes=$3
                         adm_password=$2
                         sh /user6/oracle9iAS/coalnet_screen_backup/module_backup_db $module_codes $adm_password ;; 
              
                      3) module_codes=$3
                         adm_password=$2
                         table_names=$4
                         sh /user6/oracle9iAS/coalnet_screen_backup/module_backup_tables $module_codes $adm_password $table_names ;;
                      4) sh /user6/oracle9iAS/coalnet_screen_backup/application_complete ;;
              	5) module_codes=$2
              	   sh /user6/oracle9iAS/coalnet_screen_backup/application_module $module_codes ;;
              esac
              echo "Backup Done"
              plz help me out after see my reply.......... ............

              thhhhhhhhhhhann nnnnnnnnnnnnnnn nkkkkkkkkkkkkkk kksssssssss.... .............
              Last edited by horace1; Feb 3 '07, 12:25 PM. Reason: added code tags

              Comment

              Working...