public class ExecTask {
//
// public static void main(String[] args) {
// exec();
// }
public static void exec(String runCmd,String param1,String param2){
if(runCmd==null){
log.error("run-cmd指令为空");
return;
}
log.info("执行run-cmd");
Process ps = null;
BufferedReader br=null;
try {
// 多个参数可以在param1后面继续增加,但不要忘记空格!!
String cmd = "sh "+runCmd+" "+param1+" "+param2;
log.info("cmd:{}",cmd);
ps = Runtime.getRuntime().exec(cmd);
int exitValue = ps.waitFor();
log.info("call shell exitValue:{}",exitValue);
if (0 != exitValue) {
log.error("call shell failed. error code is :" + exitValue);
}
br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
log.info(sb.toString());
log.info("run-cmd执行结束");
}
catch (Exception e) {
log.error("run-cmd异常:{}",e);
e.printStackTrace();
}finally {
try {
if (br!=null){
br.close();
}
} catch (IOException e) {
log.error("run-cmd-IOException异常:{}",e);
e.printStackTrace();
}
}
if (ps != null) {
ps.destroy();
}
}
}
|