如何使用 java 和 JSch 在 linux 上停止 tomcat 服务器?

How to stop the tomcat server on linux using java and JSch?

我有一个自动化要求,我需要停止位于 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");