Nirmal
Nirmal

Reputation: 4829

Unix Script not working in Java Process Runtime.exec()

I am developing an application in Spring Web MVC where i need to execute some of the linux script..

I am using tomcat version 5.5 for running my project in linux..

My code is looking like this :

Process proc = runtime.exec("sudo cp /var/tmp/mailserverfiles/editinterface.txt /etc/sysconfig/network-scripts/editinterface.txt");
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String line;
while ((line = bufferedreader.readLine()) != null) {
    System.out.println("\nOUTPUT = " + line);
}
System.out.print("\nbefore execute6");
try {
    if (proc.waitFor() != 0) {
       System.err.println("\nexit value = " + proc.exitValue());
    }
} catch (InterruptedException e) {
       System.err.println("\nERROR = " + e);
}

Here i want to cp a particular file from one location to another using linux script..

But when i am executing this part, i am getting

exit value = 1

as a output.. I have also tried to put this script into .sh file and try to execute that shell script here from Java Code, but i am getting same result..

Can anybody tell me, what should be the reason for this ?

Thanks in advance..

Upvotes: 1

Views: 3337

Answers (5)

Brian Agnew
Brian Agnew

Reputation: 272417

Whilst not directly answering your question, the following will help. You need to read stdout and stderr (to capture all process output), and do this concurrently to prevent blocking of the spawned process. See this answer for more info.

Upvotes: 1

nos
nos

Reputation: 229342

Assuming you can run your command from a command line, logged in as the tomcat user - try

ProcessBuilder pb = new ProcessBuilder("/usr/bin/sudo", "cp", 
                    "/var/tmp/mailserverfiles/editinterface.txt", 
                    "/etc/sysconfig/network-scripts/editinterface.txt");
pb.redirectErrorStream(true);
Process proc = pb.start();
... rest of code as before

if things still fail, start debugging. strace should be helpful. e.g. run this shell script from your java application, and figure out where things fail in the /tmp/trace.txt file:

#!/bin/sh

strace -f sudo cp /var/tmp/mailserverfiles/editinterface.txt /etc/sysconfig/network-scripts/editinterface.txt  >/tmp/trace.txt 2>&1

Upvotes: 1

Stephen C
Stephen C

Reputation: 719709

It is possible that your search path is weird and that "cp" and "sudo" are not found when you try to execute the command.

Here are some things you could try to track down your problem(s):

  • Try running the "cp" command without "sudo".

  • Try giving the full pathname of the command(s). This will avoid search path problems.

  • By default "sudo" logs failed commands using syslog(3). See if you can find traces in the corresponding logfiles.

Upvotes: 1

Will Hartung
Will Hartung

Reputation: 118804

I assume the user that Tomcat is running under has unrestricted access to sudo? And that it's not being prompted for a password?

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 994897

I would guess that sudo is expecting an interactive terminal in order to ask for a password. Since there is no interactive terminal, it prints an error message to stderr and exits with an exit code of 1. You are not reading the error stream, so you won't see any message that it might print.

You will definitely want to read the error stream in any case. Doing so now will help you diagnose what is going wrong at this point.

Upvotes: 2

Related Questions