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:
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}
}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 defaultCTRL-a
so I can still useCTRL-a
in bash to go to the beginning of a line.Use
CTRL-b c
to create shells in new windows (or just typenew
at the bash prompt to use the function). And useCTRL-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. UseCTRL-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:
make sure you set file permissions on 'code-shell' so that execution
is possible. So in my case at the CLI I didchmod a+x code-shell
allowed execution by anyone but you can make it
whatever you're comfortable with;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.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:
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
How to Find/Cut for Only The Filename from an Output of Ls -Lrt in Perl
Restart Service from Cgi Script
Grep in File and Replace String
Why Do My Keystrokes Turn into Crazy Characters After I Dump a Bunch of Binary Data into My Terminal
How to Populate /Dev with a Custom Directory with Udev
Golang Math Can Not Finished with My Code, But Python Is Ok
Perf: Strange Relation Between Software Events
How to Run a Linux Executable from Any Directory in Terminal
The Behavior When a Gnu Make Phony Target Happens to Be The Same as a Directory Name
Nohup Create New Files Nohup.Out by Day
Why This Shell Won't Work If It's Called from Rc.Local But Ssh
Bus Error Opening and Mmap'Ing a File
Format and Filter File to CSV Table
Linux Grep/Sed Certain Lines - Space Removal
How to Do an Initial Setup of Slapd Olc with Ldapmodify
Run Meteor as a Daemon Process
How to Overwrite Linux System Files into The Yocto Filesystem