功能需要所以要在jsp的後臺 用Java代碼調用.sh文件
public boolean runShell(String command,String date) throws IOException{
String command2 = command + " " + date;
Process process=Runtime.getRuntime().exec(command2);
System.out.println("開始執行shell=="+command2);
BufferedReader br = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = new String();
while ((line = br.readLine()) != null) {
System.out.println(line);
}
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
System.err.println("processes was interrupted");
return false;
}
br.close();
int ret = process.exitValue();
System.out.println(ret);
System.out.println("執行完畢!");
return true;
}
command爲.sh文件所在的絕對路徑,date是給命令傳的時間參數.如果web部署的tomcat和shell文件所在的不是同一個用戶下,就需要在shell文件裏面 先切換用戶
su - xxx -c " /home/xxx/test"