1、服务器是linux,通过类似xshell访问服务器然后命令查看项目进程是否启动
2、ShellUtils工具类如下
import com.jcraft.jsch.*;
import java.io.*;
public class ShellUtils {
/**
* 创建session
* @param host 主机名称/ip地址
* @param user 登陆用户名
* @param psw 密码
* @param port 端口
* @return
*/
public static Session getSession(String host, String user, String psw, int port){
JSch jsch=new JSch();
Session session=null;
try {
session = jsch.getSession(user, host, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(psw);
session.connect();
} catch (JSchException e) {
System.out.println("连接linux主机失败");
e.printStackTrace();
}
return session;
}
/**
* 得到可以执行命令的连接
* @param session 连接session
* @return 可以执行命令的ChannelExec
*/
public static ChannelExec getChanel(Session session){
ChannelExec openChannel=null;
try {
if(null !=session){
openChannel = (ChannelExec) session.openChannel("exec");
}
} catch (JSchException e) {
e.printStackTrace();
}
return openChannel;
}
/**
*
* @param openChannel
* @param command
* @return
*/
public static String getExcRes(ChannelExec openChannel,String command){
InputStream in =null;
BufferedReader reader=null;
StringBuffer result=new StringBuffer();
try {
try {
openChannel.setCommand(command);
int exitStatus = openChannel.getExitStatus();
System.out.println(exitStatus);
openChannel.connect();
in = openChannel.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
result.append(new String(buf.getBytes("gbk"),"UTF-8")+"
\r\n");
}
//reader.close();
} catch (JSchException e) {
result.append(e.getMessage());
e.printStackTrace();
}
} catch (IOException e) {
result.append(e.getMessage());
e.printStackTrace();
} /*finally {
//try {
//reader.close();
//} catch (IOException e) {
//e.printStackTrace();
//}
}*/
return result.toString();
}
public static ChannelShell ChannelShell(Session session) {
ChannelShell channel = null;
try {
if (null != session) {
channel = (ChannelShell) session.openChannel("shell");
channel.connect();
}
} catch (JSchException e) {
e.printStackTrace();
}
return channel;
}
public static String executeNewFlow(ChannelShell channel, String command) {
InputStream in =null;
OutputStream out=null;
String msg=null;
BufferedReader reader=null;
StringBuffer result=new StringBuffer();
try {
in = channel.getInputStream();
out = channel.getOutputStream();
out.write(command.getBytes());
out.flush();
// reader = new BufferedReader(new InputStreamReader(in));
// while ((msg =reader.readLine()) !=null ){
// System.out.println(msg);
// }
byte[] tmp=new byte[1024];
while (true){
int i=0;
//线程等待 200ms
Thread.currentThread().sleep(200);
while(in.available()>0){
i=in.read(tmp, 0, 1024);
if(i<0)break;
}
System.out.print("------------"+new String(tmp, 0, i));
return new String(tmp, 0, i);
// if(channel.isClosed()){
// if(in.available()>0) continue;
// System.out.println("exit-status: "+channel.getExitStatus());
// break;
// }
}
}catch (Exception e){
result.append(e.getMessage());
e.printStackTrace();
}
return null;
}
public static void disConnect(Session session,ChannelExec openChannel){
if(session!=null&&!openChannel.isClosed()){
openChannel.disconnect();
}
if(session!=null&&session.isConnected()){
session.disconnect();
}
}
}
Test测试类
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.Session;
import okhttp3.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class Test {
private String ip = "101.201.140.161";//服务器ip
private int port=29xxx;//端口号
private String user="xxx";//服务器账号
private String pwd="xxx";//服务器密码
private String weixin_jiqi="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=48590c4";//企业微信通知机器
private String comm="ps -ef|grep axxo;";//查看服务的进程
@org.testng.annotations.Test
public void test1(){
Session session1=ShellUtils.getSession(ip, user, pwd, port);
ChannelExec chanel=ShellUtils.getChanel(session1);
String ss=ShellUtils.getExcRes(chanel, comm);
System.out.println(ss);
if(ss!=null&&ss.length()>0&&ss.contains("Djava.awt.headless")){
System.out.println("服务启动中");
// sendMessage("axxol服务启动中",weixin_jiqi);
}else{
System.out.println("服务失败");
sendMessage("axxol服务挂了",weixin_jiqi);
}
}
/**
*
* @param message 消息体内容
* @param url 机器人的url
*/
private void sendMessage(String message,String url){
String reqBody ="{" +
" \"touser\":\"12345\"," +
" \"msgtype\":\"text\"," +
" \"agentid\":1," +
" \"text\":{" +
" \"content\":\"" +message+
" \"safe\":1" +
"}";
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)// 设置连接超时时间
.readTimeout(20, TimeUnit.SECONDS)// 设置读取超时时间
.build();
MediaType contentType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(contentType, reqBody);
Request request = new Request.Builder().url(url).post(body).addHeader("cache-control", "no-cache").build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
System.out.println();
}
}
}
pom.xml需要加入依赖插件
com.jcraft
jsch
0.1.54
com.squareup.okhttp3
okhttp
3.3.0
3、通过jenkins定时轮询:H/5 9-19 * * *,从9点到19点每隔5分钟轮询一次