Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminate remote ssh command upon disconnect

Tags:

ssh

mina

I'm using Mina SSHD client to run remote commands on OpenSSH server. I'm executing a long running command on the remote server, and would like it to terminate when the client session is closed.

when I run this command from my PC terminal:

\#ssh -t user@server sleep 12345

This what I find on the remote machine:

\# 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

when I kill the ssh client on my machine the 'sleep 12345' terminates on the remote machine.

However when I'm running the exact same using Mina Java SSH client this what I see.

    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

after code terminates the command's parent becomes the 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

Is there some flag or option in Mina to cause it to terminate my command on the remote server upon closing the session?

like image 859
Moshe Moshe Avatar asked Oct 31 '25 13:10

Moshe Moshe


1 Answers

ssh -t user@server sleep 12345

This allocates a PTY (pseudo-tty) for the remote session because of the "-t" option. When the ssh session disconnects, the PTY will send a SIGHUP to every process attached to the PTY. This causes the "sleep" process to exit.

To get the same behavior from a Mina session, request a PTY for the channel. I haven't worked with Mina before, but it looks like this is the way to do it:

ChannelExec channel = session.createExecChannel("sleep 12345");
channel.setUsePty(true);
// Optionally set the PTY terminal type, lines, and columns
OpenFuture open = channel.open();
...

setUsePty() and the other PTY functions are defined in PtyCapableChannelSession which is a parent class of ChannelExec.

like image 54
Kenster Avatar answered Nov 04 '25 01:11

Kenster