Shell 脚本 —— java 代码远程调用shell脚本重启 tomcat
个人博客网: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的更多相关文章
- eclipse mavenWeb项目真正实现热部署(修改java代码和页面文件不用重启tomcat)
1.前言 首先,本文创作灵感源于博客园园作者signheart,特此鸣谢!原文链接见文末推荐: 百度都搜破了,全网讲的都是如何将maven项目部署到tomcat上,对于热部署的认知,真 ...
- Java实践-远程调用Shell脚本并获取输出信息
1.添加依赖 <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-s ...
- Java SSH远程执行Shell脚本实现(转)
前言 此程序需要ganymed-ssh2-build210.jar包(下载地址:http://www.ganymed.ethz.ch/ssh2/) 为了调试方便,可以将\ganymed-ssh2-bu ...
- Mac笔记本中是用Idea开发工具在Java项目中调用python脚本遇到的环境变量问题解决
问题描述: mac笔记本本身会自带几个python版本,比如python2.7版本,我没有改动mac默认的python版本,只是安装了python3.7版本. 使用Pycharm开发Python项目没 ...
- java代码运行linux shell操作
1.Java调用shell Java语言以其跨平台性和简易性而著称,在Java里面的lang包里(java.lang.Runtime)提供了一个允许Java程序与该程序所运行的环境交互的接口,这就是 ...
- JMeter脚本java代码String数组要写成String[] args,不能写成String args[],否则报错。
JMeter脚本java代码String数组中括号要写在类型关键字后面,不能写在变量名后面.
- 在Eclipse中运行JAVA代码远程操作HBase的示例
在Eclipse中运行JAVA代码远程操作HBase的示例 分类: 大数据 2014-03-04 13:47 3762人阅读 评论(2) 收藏 举报 下面是一个在Windows的Eclipse中通过J ...
- 第3节 sqoop:7、通过java代码远程连接linux执行shell命令
数据库的数据同步软件sqoop 数据同步 关系型数据库到大数据平台 任务:sqoop 是批量导入数据太慢,如何做到实时的数据同步 实时的数据同步工具: canal 阿里开源的一个数据库数据实时同步的软 ...
- Java远程调用Linux脚本
参考:http://blog.csdn.net/xiao_jun_0820/article/details/26254813 http://blog.csdn.net/a19881029/artic ...
随机推荐
- 自己动手写RPC
接下来2个月 给自己定个目标 年前 自己动手做个RPC 框架 暂时技术选型 是 dotcore + netty + zookeeper/Consul
- java1.8新特性之stream
什么是Stream? Stream字面意思是流,在java中是指一个来自数据源的元素队列并支持聚合操作,存在于java.util包中,又或者说是能应用在一组元素上一次执行的操作序列.(stream是一 ...
- CF#633 D. Edge Weight Assignment
D. Edge Weight Assignment 题意 给出一个n个节点的树,现在要为边赋权值,使得任意两个叶子节点之间的路径权值异或和为0,问最多,最少有多少个不同的权值. 题解 最大值: 两个叶 ...
- 生产者消费者问题中的同步机制JAVA设计和实现
目录 问题描述 问题分析 利用记录型信号量解决 运行环境 实现思路 代码实现 运行截图 过程中出现的问题和注意点 利用AND信号集解决 运行环境 实现思路 代码实现 运行截图 问题描述 若干进程通过有 ...
- JDBC11 封装+资源配置文件管理
封装为Utils类+程序资源文件去配置 public class Utils { static Properties p=null; static { p=new Properties(); try ...
- 在ef core中使用postgres数据库的全文检索功能实战之中文支持
前言 有关通用的postgres数据库全文检索在ef core中的使用方法,参见我的上一篇文章. 本文实践了zhparser中文插件进行全文检索. 准备工作 安装插件,最方便的方法是直接使用安装好插件 ...
- mysql-kettle-superset电商可视化数据分析
1.项目概述 需求 对电商业务中的用户.商品.订单的数据进行分析,观察运营的情况 架构 业务数据库:Mysql:存储最原始的数据 ETL:Kettle 数据仓库:Mysql:存储需要进行分析处理的数据 ...
- Java ThreadLocal解析
简介 ThreadLocal 类似局部变量,解决了单个线程维护自己线程内的变量值(存.取.删),让线程之间的数据进行隔离.(InheritableThreadLocal 特例) 这里涉及三个类,Thr ...
- ScrollView 内嵌百度地图问题解决
在ScrollView上内嵌百度地图遇到两个问题 事件冲突,移动地图的时候屏幕滚动了 移动ScrollView的时候,百度地图出现黑边 问题1的处理就有各种办法了,核心都是拦截事件,我使用的办法是加一 ...
- 【雕爷学编程】MicroPython动手做(03)——零基础学MaixPy之开机测试
1.几个知识点(1)MicroPython 是 Python 3 语言的精简高效实现 ,包括Python标准库的一小部分,并针对嵌入式微控制器(单片机)和受限制的环境进行了优化,它是Python延伸出 ...