ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

java调用shell脚本,重新部署linux服务器的tomcat

2020-12-10 17:32:53  阅读:142  来源: 互联网

标签:info shell java tomcat stringBuffer pid echo append


//调用shell的方法  shellCommand 存放tomcat路径  projectname 要重新部署的war文件名
      public void executeShell(String shellCommand,String projectname) throws IOException {
        System.out.println("shellCommand:"+shellCommand);
    //存放日志路径 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(); } } } }

 shell脚本

#!/bin/bash
tomcat_home=$1
SHUTDOWN=$tomcat_home/bin/shutdown.sh
STARTTOMCAT=$tomcat_home/bin/startup.sh

echo "[info][$(date)] 正在监控tomcat0,路径:$tomcat_home"
pid=$(ps -ef | grep tomcat | grep -w 'apache-tomcat-8.5.32' | grep -v 'grep' | awk '{print $2}')
echo "[info][$(date)] pid:$pid"

if [ ‘$pid’ ]; then 
echo "[info][$(date)] tomcat1进程为:$pid"
echo "[info][$(date)] tomcat2已启动,shutdown"
$SHUTDOWN

sleep 5
pid=$(ps -ef | grep tomcat | grep -w 'apache-tomcat-8.5.32' | grep -v 'grep' | awk '{print $2}')
if [ $pid ]; then
echo "[info][$(date)] 关闭失败,准备kill进程"
kill -9 $pid
echo "[info][$(date)] kill进程完毕"
sleep 2 

else 
echo "[info][$(date)] shutdown success"
fi
 
else 
echo "[info][$(date)] tomcat3 未启动"
fi 

echo "[info][$(date)] 启动tomcat5"
$STARTTOMCAT

  

标签:info,shell,java,tomcat,stringBuffer,pid,echo,append
来源: https://www.cnblogs.com/luyue0220/p/14116307.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有