Keep Ssh Sessions Running After Disconnection

Keep SSH Sessions running after disconnection

It's not really possible after you've already started a session. But for new sessions you can do the following:

  1. Add the following to the top of your .bash_profile:

    if [ -z "${PS1}" ] ; then
    return
    fi

    if [ "${TERM}" != "screen" ] ; then
    export HOSTNAME
    exec screen -xRR
    fi

    function new {
    u=${1:-$USER}
    test ${u} = ${USER} && screen -t ${u}@${HOSTNAME} || screen -t ${u}@${HOSTNAME} su --login ${u}
    }
  2. Put the following content into .screenrc:

    escape ^bb
    shell -$SHELL
    termcapinfo xterm ti@:te@
    hardstatus lastline "%-Lw[%n%f %t]%+Lw%<"
    screen -t ${USER}@${HOSTNAME}

    These are mostly my own customizations of screen. The most important of which is that I set the screen escape character to CTRL-b instead of the default CTRL-a so I can still use CTRL-a in bash to go to the beginning of a line.

  3. Use CTRL-b c to create shells in new windows (or just type new at the bash prompt to use the function). And use CTRL-b d to detach your session and leave it running. Next time you login, you'll be reattached to your session and everything will be as it was. Use CTRL-b n to cycle through the windows you've created. If you don't want to multiple windows, you don't have to, just use the ability to leave a session running and reattach later.

Keep SSH Sessions running after disconnection - for overnight

Run the screen on the server (as opposed to on the client, which is what you seem to be doing right now). This way, MATLAB can write output even if you are not connected to the server via ssh. The order of commands for this is ssh, screen, matlab. If you want to resume your session, just connect to the server via ssh, then run something like screen -x

VS Code remote ssh: how to allow processes to keep running to completion after disconnect?

As noted I am working VS Code in a remote Ubuntu environment that I ssh in to. If you are doing so follow the steps that are outlined by Moreno (see link in question) BUT because I am working in a Linux environment I had to adjust some items in the script. The adjustments are:

  1. make sure you set file permissions on 'code-shell' so that execution
    is possible. So in my case at the CLI I did chmod a+x code-shell allowed execution by anyone but you can make it
    whatever you're comfortable with;

  2. make the shebang line in your script use /usr/bin/env bash
    which will choose the first permissible shell your environment will
    allow. Using /bin/bash did not work for me.

  3. a slight change to the naming portion of the script. I made it
    'vscode' followed by the first 3 characters of md5 sum. That makes
    new terminals distinguishable.

The final script which works for me is:
Sample Image

So the whole process is: set up for ssh remote in VS Code -> adjust VS Code settings in workspace to run modified terminal (code-shell) -> create code-shell script with required permissions. If you get a disconnect running processes will continue uninterrupted.

Keep SSH session alive

The ssh daemon (sshd), which runs server-side, closes the connection from the server-side if the client goes silent (i.e., does not send information). To prevent connection loss, instruct the ssh client to send a sign-of-life signal to the server once in a while.

The configuration for this is in the file $HOME/.ssh/config, create the file if it does not exist (the config file must not be world-readable, so run chmod 600 ~/.ssh/config after creating the file). To send the signal every e.g. four minutes (240 seconds) to the remote host, put the following in that configuration file:

Host remotehost
HostName remotehost.com
ServerAliveInterval 240

To enable sending a keep-alive signal for all hosts, place the following contents in the configuration file:

Host *
ServerAliveInterval 240

Windows ssh - How to keep proccess running after Disconnect

If you have GNU Coreutils/cygwin installed then you can run start nohup your-command > logfile.txt and it should remain running when you exit the ssh session. This works with the OpenSSH server that comes with Windows 10.

Keep SSH session alive while computer sleep?

I found the answer it depends on tcp keepalive settings:

For the list of available TCP settings (FreeBSD 4.8 an up and 5.4):

sysctl -A | grep net.inet.tcp
  • net.inet.tcp.keepidle - Amount of time, in milliseconds, that the (TCP) connection must be idle before keepalive probes (if enabled) are sent.

  • net.inet.tcp.keepintvl - The interval, in milliseconds, between keepalive probes sent to remote machines. After TCPTV_KEEPCNT (default 8) probes are sent, with no response, the (TCP)connection is dropped.

  • net.inet.tcp.always_keepalive - Assume that SO_KEEPALIVE is set on all TCP connections, the kernel will periodically send a packet to the remote host to verify the connection is still up.

Therefore formula to calculate maximum TCP inactive connection time is following:

net.inet.tcp.keepidle + (net.inet.tcp.keepintvl x 8)

the result is in milliseconds. Therefore, by setting

net.inet.tcp.keepidle = 10000
net.inet.tcp.keepintvl = 5000
net.inet.tcp.always_keepalive = 1 (must be 1 always)

the system will disconnect a call when TCP connection is dead for: 10000 + (5000 x 8) = 50000 msec (50 sec). To make system remember these settings at startup, you should add them to /etc/sysctl.conf file



Related Topics



Leave a reply



Submit