executing a script from linux in Java

Hi,
I'm new to linux and I want to ask you how can I run a script written in linux ?
The script is "start0" and is working from linux environment. But when I execute the following method , it gives me some strange errors:
 
private void start(int nrCalc)
{
 try{
 Runtime runtime=Runtime.getRuntime();
 runtime.exec("/home/sionut/cafe/start0 "+nrCalc);
 }catch(IOException e)
 {
  out("An error with IO");
 }
}
 
Here's the script:
 
echo $1 >> /home/sionut/cafe/start.out
 
and the error is:
 
Reaped pid=14415, status =255
and then it gives me an error .
 
PS: Another question, not related to this. When I type "ps" and my program is running , it shows six times "java". My program doesn't start any child processes, so is it an error or it's normal ?
scutaru_ionutAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JodCommented:
How are you calling the Start routine to create your process? And what is the error you get?

Do you normally get many java processes or do you normally just have one? the reason I ask is that the subprocess you have created is neccessarily killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously. Your processes may be continuing...

The Process may also be being killed off or affected before finishing because your code does not wait for them. Maybe you need to wait? Worth trying to get it working.

Also remember that for a process started from java by default all its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()).

This leads on to a number of things to try based on retrieving the process object if you:

1) use waitfor() on the process object to wait until the process is finished

2) use exitValue() to see what the program return value is

3) use getOutputStream() on the process object to see what it is being produced


So in general try something more like this:

Runtime runtime = Runtime.getRuntime();
Process p = Runtime.exec("xxx");
p.waitFor();

until you have figured out what is going wrong. You will need to catch an exception on waitFor()...
0
JodCommented:
Here is a full example of reading the output of you script file:

import java.io.*;

public class CmdExec {

       public CmdExec(String cmdline) {
               try {
                       String line;

                       Process p = Runtime.getRuntime().exec(cmdline);
                       DataInputStream input = new DataInputStream(
                                                       p.getInputStream());
                       while ((line = input.readLine()) != null) {
                               System.out.println(line);
                       }
                       input.close();
               } catch (Exception err) {
                       System.out.println("EXEC failed: " + err.toString());
                       err.printStackTrace();
               }
       }

       public static void main(String argv[]) {
               new CmdExec(argv[0]);
       }

}
 

0
scutaru_ionutAuthor Commented:
The error I get is an SQL Exception which don't show up if I don't execute the script. It seems strange. I don't know why the catch block catches the exception, because the exception doesn't show up from SQL.
I'll try the waitFor() method ....
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

JodCommented:
SQL Exception?

There is more than meets the naked eye here....

You should not be getting an SQL exception unless you are doing something with SQL.

What elese are you doing in your code before or after calling the script?
0
scutaru_ionutAuthor Commented:
I made another simple test. Here it is:



import java.io.*;

public class Test
{
public Test()
{
  try{
  Runtime r=Runtime.getRuntime();
  Process p=r.exec("/bin/bash /home/sionut/cafe/start0 "+2);
  p.waitFor();
  }
  catch(IOException e)
  {
    System.out.println("Error 1");
   }
  catch(InterruptedException e)
  {
    System.out.println("Error 2");
  }
  catch(Exception e)
 {
    System.out.println("Error 2");
 }
}

public static void main(String args[])
{
      new Test();
}
}


And this program is working just sometimes. SOmetimes it just hangs, sometimes it gives me those strange messages (Reapid...) and sometimes works just fine. I'm using Linux 6.1
Also, the script in linux is working just fine and the path to it is good. Some people told me that I shoul call the script as an argument to the /bin/bash.
So ...
0
JodCommented:
import java.io.*;

public class Test
{
Runtime r = null;
 
public Test() {
  r=Runtime.getRuntime();
}

public runScript()
{
  try{
    Process p = r.exec("/bin/bash /home/sionut/cafe/start0 "+2);
    p.waitFor();

  } catch (Exception e) {
    System.out.println("Problems...\n" + e);
  }
}

public static void main(String args[])
{
  Test t = new Test();
  t.runScript();  
}

}

or even just:


public class Test
{


  public static void main(String args[]) {
    try {
      Runtime r=Runtime.getRuntime();

      // try with both these...

      //  Process p = r.exec("/home/sionut/cafe/start0 "+2);
      Process p = r.exec("/bin/bash /home/sionut/cafe/start0 "+2);

      p.waitFor();

    } catch (Exception e) {
      System.out.println("Problems...\n" + e);
    }
  }

}
0
JodCommented:
Try with the two examples above and tell me what happens...
0
scutaru_ionutAuthor Commented:
It's trhe same thing. Sometimes it works just fine, sometimes it works but gives me the Reapid .. thing and sometimes it just hangs up. But if I press CTRL+C to stop the process, the work is done, i.e. the number 2 is already in the file. But what is it waiting for ?
I think it's something about java or about Linux 6.1. I think I'll use something else..
0
JodCommented:
Sounds like your process is not terminating for some reason. Any idea wht this might be?

Is there any other process looking at or locking the files you are trying to copy in your script?

It is probably not a java thing - I have never had a problem with exec.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.