0

The classic story goes:

I have a terminal emulator with multiple tabs/screens. In each tab, I start an SSH session to a remote server. I start possibly long-running interactive jobs in those sessions. A connection to the remote server is broken, the tab affected now shows the local shell, I reconnect to the server by starting a new SSH session to find the interactive job on the server dead.

Now, the canonical answer to this is: use screen or tmux. Apart from just "saving" your session, there are also features such as multiple tabs, etc. But why would I want multiple tabs if this is already in a multiple-tab environment of my local terminal emulator app?

The question is: Can I have persistent remote SSH sessions inside the tabs/screens of my terminal emulator WITHOUT having to manually start (or attach to existing) screen or similar? Can there be a wrapper to the SSH session that runs in each of my tabs, such that it manages the persistence of the session? (much like screen does for local shell)

So ideally: I create a new tab in my terminal emulator, start an remote ssh session, do long-running work, get disconnected, reconnect and the tab automagically reattaches and I see my work continuing. So like byobu but the tabs are native tabs of my terminal emulator.

vedran
  • 101
  • 1
    You can always add code to your .bashrc on the remote end to start/attach tmux automatically. – AlexD Feb 14 '24 at 11:51
  • you might want interessted into mosh server which can continue the session in case of disconnection but it might require clients, that understand and support it. moreover i aint know if you seen this similar question – djdomi Feb 14 '24 at 13:22

3 Answers3

1

Short Answer: YES... but it depends

on what you understand on long term and uninterrupted.

Using additional Software

Using the mosh-shell which is part from mosh-server.
Using a compatible SSH client, it is possible to connect to the server even when the network connection is interrupted, such as when using a mobile phone on a interruptible LTE/4G or different WiFi's.

The setup is on debian straightforward:

  • apt install mosh-server

Using the default settings will allow compatible clients (like JuiceSSH on Android) to use these features. You remain connected or are reconnected to the same session.

The native Solution

But in a Generally looking way, without interruption and without using Virtual Terminals like Screen, tmux, byod or Similar, you may want to run your from current state of the question, your hidden command or programm, with

  • ssh foo@bar.local "command &"

This usually detach the command into the background. Which will enable you to run it, without staying connected.

If the command is still running, you may want to use

  • [jobs] -l

to attach to it again, you may want to use

  • [fg]

In the default state, you may need to use work arounds. Telnet or SSH was not yet designed to reconnect to the same session.

You may want to use the Feature in the file

  • /etc/ssh/sshd_config and add
  • TCPKeepAlive Yes You also need to use on the client in the file
  • /etc/ssh/ssh_config and add
  • ServerAliveInterval 60 which allows the sending of a null packet and keeps the connection alive. This does not prevent a total failure, but it can reduce the number of inactive sessions being disconnected.

as last noted: In the current state of the Question is unknown, what command will be started. Therefore, I can currently only assume general solutions.

djdomi
  • 2,020
0

Short answer: No.

While you could spend a lot of time addressing elements of the problem, you're not going to get something which works the way you want.

If your local terminal supports scripting (the terminal - not the shell its accessing) and you are reasonably competent at scripting in expect then you could write something to ssh / poll existing tmux/screen sessions / open tabs to connect to each of these.

symcbean
  • 22,376
0

I might not have properly understood your requirements, but are you already aware of abduco(1) and dtach(1)?

abduco provides a way to disconnect a given application from its controlling terminal, thus it provides roughly the same session attach/detach support as screen(1), tmux(1) or dtach(1).

dtach is a program that emulates the detach feature of screen. It is designed to be transparent and un-intrusive; it avoids interpreting the input and output between attached terminals and the program under its control. Consequently, it works best with full-screen applications such as emacs.

dtach is intended for users who want the detach feature of screen without the other overhead of screen. It is tiny, does not use many libraries, and stays out of the way as much as possible.

A session in dtach is a single instance in which a program is running under the control of dtach. The program is disassociated from the original terminal, and is thus protected from your original terminal being disconnected for some reason.

Other instances of dtach can attach themselves to a particular session. Input and output is copied between the program running in the dtach session, and the attached terminals.

Alexis
  • 101
  • The person in charge said that no software should be used to attach or unattach sessions. thereforce, yeh its a Valid suggestion but does not meet the request. – djdomi Feb 15 '24 at 15:00