我有一个自动化要求,我需要停止位于 Linux 的 tomcat 服务器。我该怎么做?我正在使用 JSch 尝试多个代码片段,但这些代码片段没有按预期工作。有人可以帮我吗?
这是我的示例代码 -
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("CONNECTED!");
String process = serverGetprocess();
LOG.info("ACTAUL SERVER PROCESS "+process);
commandStopServer = "kill -9" + process;
LOG.info(commandStopServer);
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(commandStopServer);
channel.setInputStream(null);
channel.setOutputStream(System.out, true);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
Thread.sleep(1000);
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
LOG.info("Inside catch of channel closing!");
}
}
channel.disconnect();
return true;
最佳答案
这里是放置在设置命令“pkill -9 -f tomcat”中的代码,即;
((ChannelExec) channel).setCommand("pkill -9 -f tomcat");
关于java - 如何使用java和JSch停止linux上的tomcat服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51356446/