我正在使用Mina SSHD客户端在OpenSSH服务器上运行远程命令。 我正在执行一个长时间运行的命令,在客户端会话关闭时希望它终止。
当我从我的PC终端运行此命令:
\#ssh -t user@server sleep 12345
这是我在远程机器上发现的内容:
\# ps -axf
---- Omitted for clarity
12158 ? Ss 0:29 /usr/sbin/sshd -4
22708 ? Ss 0:11 \\_ sshd: user@pts/3,pts/4
16894 pts/3 Ss 0:00 | \\_ -bash
17750 pts/3 R+ 0:00 | | \\_ ps -axf
17606 pts/4 Ss+ 0:00 | \\_ sleep 12345
---- Omitted for clarity
当我在本机上杀死ssh客户端时,“sleep 12345”将终止在远程计算机上。
但是,当我使用Mina Java SSH客户端运行完全相同的内容时,我看到了这个。
SshClient client = SshClient.setUpDefaultClient();
client.start();
ConnectFuture connect = client.connect("user", "server", 22);
connect.await(10000); //ms
ClientSession session = connect.getSession();
session.addPasswordIdentity("password");
AuthFuture auth = session.auth();
auth.await(10000);
ClientChannel channel = session.createExecChannel("sleep 12345");
OpenFuture open = channel.open();
open.await(10000);
Thread.sleep(15000); // ms, wait for command to run
channel.close(true);
session.close(true);
client.stop();
\# ps -axf
---- Omitted for clarity
27364 ? Ss 0:00 \\_ sshd: user@pts/0
3277 pts/0 Ss 0:00 | \\_ -bash
22306 pts/0 R+ 0:00 | \\_ ps axf
21699 ? Ss 0:00 \\_ sshd: user@notty
21796 ? Ss 0:00 \\_ sleep 12345
---- Omitted for clarity
在代码完成后,命令的父进程会变成 init 进程的 PID:
\# ps -axf
21796 ? Ss 0:00 sleep 12345
\#ps -ef | grep sleep
root 21796 1 0 08:26 ? 00:00:00 sleep 12345
在Mina中是否有某个标志或选项,可以在关闭会话时终止对远程服务器的命令?