//调用shell的方法 shellCommand 存放tomcat路径 projectname 要重新部署的war文件名
public void executeShell(String shellCommand,String projectname) throws IOException {
//存放日志路径
String executeShellLogFile=shellCommand+"\\executeShell.log";
//调用的shell路径
String sendKondorShellName=shellCommand+projectname+".sh";
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferedReader = null;
// 格式化日期时间,记录日志时使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");
try {
stringBuffer.append(dateFormat.format(new Date())).append("准备执行Shell命令 ").append(sendKondorShellName).append(" \r\n");
Process pid = null;
//String[] cmd = { "/bin/sh", "-c", sendKondorShellName };
String[] cmd = { "sh", sendKondorShellName, shellCommand };
//给shell传递参数
//String[] cmd = { "/bin/sh", "-c", shellCommand+" paramater" };
// 执行Shell命令
pid = Runtime.getRuntime().exec(cmd);
if (pid != null) {
stringBuffer.append("进程号:").append(pid.toString()).append("\r\n");
// bufferedReader用于读取Shell的输出内容
bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);
pid.waitFor();
} else {
stringBuffer.append("没有pid\r\n");
}
stringBuffer.append(dateFormat.format(new Date())).append("Shell命令执行完毕\r\n执行结果为:\r\n");
String line = null;
// 读取Shell的输出内容,并添加到stringBuffer中
while (bufferedReader != null && (line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\r\n");
}
System.out.println("stringBuffer:"+stringBuffer);
} catch (Exception ioe) {
stringBuffer.append("执行Shell命令时发生异常:\r\n").append(ioe.getMessage()).append("\r\n");
} finally {
if (bufferedReader != null) {
OutputStreamWriter outputStreamWriter = null;
try {
bufferedReader.close();
// 将Shell的执行情况输出到日志文件中
OutputStream outputStream = new FileOutputStream(executeShellLogFile);
outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
outputStreamWriter.write(stringBuffer.toString());
System.out.println("stringBuffer.toString():"+stringBuffer.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStreamWriter.close();
}
}
}
}