我有一个使用“catalina.sh start”启动 tomcat 的 shell 脚本。该脚本运行良好并启动 tomcat,即使我退出 ssh session ,它也会保持运行状态。
我正在尝试从 Jsch 调用此脚本
shell = new JSch();
session = shell.getSession(user, host, SSH_DEFAULT_PORT);
session.setConfig(config);
session.setPassword(password);
session.connect();
commandChannel = (ChannelExec) session.openChannel("exec");
commandChannel.setCommand(command);
commandChannel.setPty(true);
commandChannel.connect();
这可以正常调用我的脚本并且 tomcat 启动,但是一旦进程退出,tomcat 就会收到关闭信号并关闭。我尝试使用 setDaemonThread 但没有成功。知道为什么会发生这种情况吗?
更新:该脚本还执行一些与启动 tomcat 无关的 sudo 操作,因此它需要 tty。用户在 sudoers 中设置,不需要密码,因此不需要提示。
最佳答案
我认为您不应该为该命令分配 pty 以使该命令在后台运行而不附加到终端。如果您改为 commandChannel.setPty(false)
会发生什么?
编辑:您实际上正在运行 sudo 并在脚本中提供密码吗?难道你不应该以 root 身份运行它吗?
如果您想要使用 sudo,请在运行命令后使用 disown
或 nohup
( explanation )
关于java - Jsch 退出后进程终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3783441/