String scriptPath = Property.getProperty("shell.path") + Property.getProperty(shellName);
//scriptPath 脚本执行绝对路径 date db是传入参数,可以多个
String[] cmd = new String[]{scriptPath, date,db};
private void execute(String [] cmd , String scriptPath) {
String result = "";
BufferedReader bufferedReader = null;
BufferedReader br = null;
try {
//解决脚本没有执行权限
ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", scriptPath);
Process process = builder.start();
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String str;
while ((str = bufferedReader.readLine()) != null) {
stringBuffer.append(str).append("\n");
}
result = stringBuffer.toString();
log.info(result);
process.waitFor();
log.info("builder创建完毕,正式执行脚本进行补数操作! ");
Process ps = Runtime.getRuntime().exec(cmd);
// 此处是为了解决脚本执行之后,缓冲区满,卡住的问题
br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
// StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
// sb.append(line).append("\n");
}
// result = sb.toString();
ps.waitFor();
log.info("脚本执行完毕! ");
} catch (Exception e) {
log.error("脚本执行异常! cmd:{},path:{}" ,cmd,scriptPath, e);
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}