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

1、创建maven 工程

maven 依赖:

        <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.13.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/logutil/logutil --> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>

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

package com.webcode.cn;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import 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  脚本文件

#!/bin/bash

#kill tomcat pid

pidlist=`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

Shell 脚本 —— java 代码远程调用shell脚本重启 tomcat的更多相关文章

  1. eclipse mavenWeb项目真正实现热部署(修改java代码和页面文件不用重启tomcat)

            1.前言 首先,本文创作灵感源于博客园园作者signheart,特此鸣谢!原文链接见文末推荐: 百度都搜破了,全网讲的都是如何将maven项目部署到tomcat上,对于热部署的认知,真 ...

  2. Java实践-远程调用Shell脚本并获取输出信息

    1.添加依赖 <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-s ...

  3. Java SSH远程执行Shell脚本实现(转)

    前言 此程序需要ganymed-ssh2-build210.jar包(下载地址:http://www.ganymed.ethz.ch/ssh2/) 为了调试方便,可以将\ganymed-ssh2-bu ...

  4. Mac笔记本中是用Idea开发工具在Java项目中调用python脚本遇到的环境变量问题解决

    问题描述: mac笔记本本身会自带几个python版本,比如python2.7版本,我没有改动mac默认的python版本,只是安装了python3.7版本. 使用Pycharm开发Python项目没 ...

  5. java代码运行linux shell操作

    1.Java调用shell  Java语言以其跨平台性和简易性而著称,在Java里面的lang包里(java.lang.Runtime)提供了一个允许Java程序与该程序所运行的环境交互的接口,这就是 ...

  6. JMeter脚本java代码String数组要写成String[] args,不能写成String args[],否则报错。

    JMeter脚本java代码String数组中括号要写在类型关键字后面,不能写在变量名后面.

  7. 在Eclipse中运行JAVA代码远程操作HBase的示例

    在Eclipse中运行JAVA代码远程操作HBase的示例 分类: 大数据 2014-03-04 13:47 3762人阅读 评论(2) 收藏 举报 下面是一个在Windows的Eclipse中通过J ...

  8. 第3节 sqoop:7、通过java代码远程连接linux执行shell命令

    数据库的数据同步软件sqoop 数据同步 关系型数据库到大数据平台 任务:sqoop 是批量导入数据太慢,如何做到实时的数据同步 实时的数据同步工具: canal 阿里开源的一个数据库数据实时同步的软 ...

  9. Java远程调用Linux脚本

    参考:http://blog.csdn.net/xiao_jun_0820/article/details/26254813  http://blog.csdn.net/a19881029/artic ...

随机推荐

  1. C# 中 枚举Enum 一些转换的方法整理

    工作中 经常遇到枚举 的一些转换  特别是获取枚举备注等  特地整理下 方法以后使用 public void TestMethod1() { TestEnumOne colorEnum = TestE ...

  2. [hdu5255]枚举

    思路:这题与csu1392题目类似,方法类似.枚举最高位,最低位和中间数字的长度,然后列等式,计算中间的数字,看长度是不是跟枚举的一致,需要注意的是中间数字可以有前导0,如果根据等式算出来的中间数字为 ...

  3. 一道题带你搞定Python函数中形参和实参问题

    昨天在Python学习群里有位路人甲问了个Python函数中关于形参和实参一个很基础的问题,虽然很基础,但是对于很多小白来说不一定简单,反而会被搞得稀里糊涂.人生苦短,我用Python. 为了解答大家 ...

  4. @vue/cli 4.0.5 学习记录

    1. Vue CLI (@vue/cli) 是一个全局安装的 npm 包,提供了终端里的 vue 命令.Vue CLI 插件的名字以 @vue/cli-plugin- (内建插件) 或 vue-cli ...

  5. DPDK IP分片及重组库(学习笔记)

    1 前置知识学习 1.1 MTU MTU是最大传输单元( Maximum Transmission Unit)的缩写,指一个接口无需分片所能发送的数据包的最大字节数.  MTU范围在46 ~ 1500 ...

  6. redis起步 zz

    Rdis和JQuery一样是纯粹为应用而产生的,这里记录的是在CentOS 5.7上学习入门文章: 1.Redis简介 Redis是一个key-value存储系统.和Memcached类似,但是解决了 ...

  7. select 后台获取text 并根据text 设置value选中项

    -------获取select 的text string orderNo = this.orderNo.Items[this.orderNo.SelectedIndex].Text; -----根据t ...

  8. Redis学习笔记(十三) 复制(下)

    上一篇写了Redis复制功能的简单应用,下面我们看下Redis复制功能的实现过程.下面基本上是理论部分,枯燥乏味,但希望大家能看看,毕竟知识不都是感兴趣的.耐得住寂寞,经得起诱惑,方能守得住繁华 ~. ...

  9. 转义URL 含有中文和特殊符号

    方法1: //这个方法被废弃了 NSString *urlString = @"https://www.cnblogs.com/huaida/#/程序员"; NSString* e ...

  10. Spring Boot 教程(2) - Mybatis

    Spring Boot 教程 - Mybatis 1. 什么是Mybatis? MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射.MyBatis 免除了几乎所有的 J ...