views:

324

answers:

5

Hello All...

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..

+2  A: 

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.

Greg Hewgill
Ya, but i have added tomcat user in visudo with No Password options..
Nirmal
That's great. What does the error stream say?
Greg Hewgill
It's giving me nothing..
Nirmal
In the simple case, like "cp /tmp/editinterface.txt /tmp/editinterface1.txt" it's also not working.. from .exec().
Nirmal
I'm not prepared to believe that the command you're running exits with exit value 1 *and* doesn't print anything at all to either stdout or stderr. What happens if you don't do all that BufferedReader stuff and just read from the InputStream directly?
Greg Hewgill
When i am directly reading from inputStream of proc, it's giving me -1.
Nirmal
+1  A: 

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

Will Hartung
ya but for that i have added tomcat user is visudo.. with No Password options...
Nirmal
+1  A: 

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.

Stephen C
+1  A: 

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
nos
+1  A: 

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.

Brian Agnew