Shell 脚本 —— java 代码远程调用shell脚本重启 tomcat

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

1、创建maven 工程

maven 依赖:

1 <dependency> 2 <groupId>commons-io</groupId> 3 <artifactId>commons-io</artifactId> 4 <version>2.1</version> 5 </dependency> 6 <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> 7 8 <!-- https://mvnrepository.com/artifact/junit/junit --> 9 <dependency> 10 <groupId>junit</groupId> 11 <artifactId>junit</artifactId> 12 <version>4.11</version> 13 <scope>test</scope> 14 </dependency> 15 <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> 16 <dependency> 17 <groupId>org.springframework</groupId> 18 <artifactId>spring-test</artifactId> 19 <version>5.0.13.RELEASE</version> 20 <scope>test</scope> 21 </dependency> 22 <!-- https://mvnrepository.com/artifact/logutil/logutil --> 23 24 <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> 25 <dependency> 26 <groupId>org.apache.logging.log4j</groupId> 27 <artifactId>log4j-core</artifactId> 28 <version>2.11.1</version> 29 </dependency>

2、创建 RemoteShellExecutor. java  文件用于远程连接Linux 服务器

1package com.webcode.cn; 2 3import ch.ethz.ssh2.ChannelCondition; 4import ch.ethz.ssh2.Session; 5import ch.ethz.ssh2.StreamGobbler; 6import org.apache.commons.io.IOUtils; import ch.ethz.ssh2.Connection; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; public class RemoteShellExecutor { private Connection conn; /** 远程机器IP */ private String ip; /** 用户名 */ private String osUsername; /** 密码 */ private String password; private String charset = Charset.defaultCharset().toString(); private static final int TIME_OUT = 1000 * 5 * 60; /** * 构造函数 * @param ip * @param usr * @param pasword */ public RemoteShellExecutor(String ip, String usr, String pasword) { this.ip = ip; this.osUsername = usr; this.password = pasword; } /** * 登录 * @return * @throws IOException */ private boolean login() throws IOException { conn = new Connection(ip); conn.connect(); return conn.authenticateWithPassword(osUsername, password); } /** * 执行脚本 * * @param cmds * @return * @throws Exception */ public int exec(String cmds) throws Exception { InputStream stdOut = null; InputStream stdErr = null; String outStr = ""; String outErr = ""; int ret = -1; try { if (login()) { // Open a new {@link Session} on this connection Session session = conn.openSession(); // Execute a command on the remote machine. session.execCommand(cmds); stdOut = new StreamGobbler(session.getStdout()); outStr = processStream(stdOut, charset); stdErr = new StreamGobbler(session.getStderr()); outErr = processStream(stdErr, charset); session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT); System.out.println("outStr=" + outStr); System.out.println("outErr=" + outErr); ret = session.getExitStatus(); } else { throw new Exception("登录远程机器失败" + ip); // 自定义异常类 实现略 } } finally { if (conn != null) { conn.close(); } IOUtils.closeQuietly(stdOut); IOUtils.closeQuietly(stdErr); } return ret; } /** * @param in * @param charset * @return * @throws IOException * @throws UnsupportedEncodingException */ private String processStream(InputStream in, String charset) throws Exception { byte[] buf = new byte[1024]; StringBuilder sb = new StringBuilder(); while (in.read(buf) != -1) { sb.append(new String(buf, charset)); } return sb.toString(); } public static void main(String args[]) throws Exception { RemoteShellExecutor executor = new RemoteShellExecutor("192.168.2xx.118", "root", "15989xxxxxx***"); // 执行myTest.sh 参数为java Know dummy System.out.println(executor.exec("/home/wenmin/kill-tomcat.sh")); } }

3、在Linux  /home/wenmin 目录下创建 kill-tomcat.sh  脚本文件

1#!/bin/bash 2 3#kill tomcat pid 4 5pidlist=`ps -ef|grep apache-tomcat-7.0.75|grep -v "grep"|awk '{print $2}'` #找到tomcat的PID号 echo "tomcat Id list :$pidlist" //显示pid kill -9 $pidlist #杀掉改进程 echo "KILL $pidlist:" //提示进程以及被杀掉 echo "service stop success" echo "start tomcat" cd /opt/apache-tomcat-7.0.75 pwd rm -rf work/* cd bin ./startup.sh #;tail -f ../logs/catalina.out

4、执行 main  方法

J:\folder\JDK1.8\bin\java -javaagent:J:\P2C\ideaIU-2017.2.win\lib\idea_rt.jar=62052:J:\P2C\ideaIU-2017.2.win\bin -Dfile.encoding=UTF-8 -classpath J:\folder\JDK1.8\jre\lib\charsets.jar;J:\folder\JDK1.8\jre\lib\deploy.jar;J:\folder\JDK1.8\jre\lib\ext\access-bridge-64.jar;J:\folder\JDK1.8\jre\lib\ext\cldrdata.jar;J:\folder\JDK1.8\jre\lib\ext\dnsns.jar;J:\folder\JDK1.8\jre\lib\ext\jaccess.jar;J:\folder\JDK1.8\jre\lib\ext\jfxrt.jar;J:\folder\JDK1.8\jre\lib\ext\localedata.jar;J:\folder\JDK1.8\jre\lib\ext\nashorn.jar;J:\folder\JDK1.8\jre\lib\ext\sunec.jar;J:\folder\JDK1.8\jre\lib\ext\sunjce_provider.jar;J:\folder\JDK1.8\jre\lib\ext\sunmscapi.jar;J:\folder\JDK1.8\jre\lib\ext\sunpkcs11.jar;J:\folder\JDK1.8\jre\lib\ext\zipfs.jar;J:\folder\JDK1.8\jre\lib\javaws.jar;J:\folder\JDK1.8\jre\lib\jce.jar;J:\folder\JDK1.8\jre\lib\jfr.jar;J:\folder\JDK1.8\jre\lib\jfxswt.jar;J:\folder\JDK1.8\jre\lib\jsse.jar;J:\folder\JDK1.8\jre\lib\management-agent.jar;J:\folder\JDK1.8\jre\lib\plugin.jar;J:\folder\JDK1.8\jre\lib\resources.jar;J:\folder\JDK1.8\jre\lib\rt.jar;J:\IDEA_Work_Space\shell-java\target\classes;C:\Users\wushaopei\.m2\repository\org\jvnet\hudson\ganymed-ssh2\build210-hudson-1\ganymed-ssh2-build210-hudson-1.jar;C:\Users\wushaopei\.m2\repository\commons-io\commons-io\2.1\commons-io-2.1.jar;C:\Users\wushaopei\.m2\repository\org\apache\logging\log4j\log4j-core\2.11.1\log4j-core-2.11.1.jar;C:\Users\wushaopei\.m2\repository\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar com.webcode.cn.RemoteShellExecutor outStr=tomcat Id list :14141 //显示pid KILL 14141: //提示进程以及被杀掉 service stop success start tomcat /opt/apache-tomcat-7.0.75 Using CATALINA_BASE: /opt/apache-tomcat-7.0.75 Using CATALINA_HOME: /opt/apache-tomcat-7.0.75 tart tomcat /opt/apache-tomcat-7.0.75 Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.75/temp CATALINA_HOME: /opt/apache-tomcat-7.0.75 tart tomcat /opt/apache-tomcat-7.0.75 Using JRE_HOME: /usr Using CLASSPATH: /opt/apache-tomcat-7.0.75/bin/bootstrap.jar:/opt/apache-tomcat-7.0.75/bin/tomcat-juli.jar Tomcat started. /usr Using CLASSPATH: /opt/apache-tomcat-7.0.75/bin/bootstrap.jar:/opt/apache-tomcat-7.0.75/bin/tomcat-juli.jar outErr= 0 Process finished with exit code 0
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )