I have a java program which runs on Linux and attempts to call one shell script. This shell script tries to copy a local directory to another server. The way to call the script is :
bash copy_logs.sh "TestResults/07232015042652"
When i call this from linux terminal, the directory gets copied to remote server successfully. Now i want to execute this from Java program. My code snippet is as follows :
Sting direc = "07232015042652"
p = Runtime.getRuntime().exec("bash copy_logs.sh \"TestResults/"+direc+"\"");
When i run this, though i do not get any error or exception but the file never gets copied.
Contents of copy_logs.sh is as follows :
scp -r $TS test@192.168.199.57:/home/apacheweb/html/Weekly_Status/prov_check/
if [ $? -eq 0 ]
then
echo "File copied successfully"
else
echo "Failed to copy"
I tried to print the result of execution by
InputStream is = p.getInputStream();
StringBuilder sb = new StringBuilder();
int i=0;
try {
while ((i=is.read())!=-1){
sb.append((char)i);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(sb);
And i get the output : Failed to copy . Just wondering what can be root cause of this???
Got it working by :
ProcessBuilder pb = new ProcessBuilder("bash", "-c", "/bin/bash copy_logs.sh \"TestResults/"+directoryName+"\"");
Process shell = pb.start();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.