1

I'm using Ubuntu 18.04 (for both my local machine and my server) and the terminal freezes when I come back to an open SSH session after leaving it alone for a while.

I have to close the terminal window and SSH back into the server to get access again. It's becoming quite annoying.

If the SSH session is displaying a stream of new information, like a log that prints new info every couple of seconds, then it will not freeze. The terminal only freezes if nothing has been printed to the screen for a while.

How can I stay logged into an SSH session without this happening?

1

1 Answer 1

0

I use the following to keep an SSH session alive:

#!/usr/bin/perl
# Name: /usr/bin/bigben         Author: Joe Smith 
# Purpose: Outputs time, uptime, or message every N minutes to keep session up.
# Automatically terminates when STDIN is no longer connected to a terminal.

use strict; use warnings;
$| = 1;

my $period = 10;        # Default minutes between outputs
@ARGV and $ARGV[0] =~ /^-(\d+)$/ and $period = 0-shift; # "-60" for 60 minutes

(@ARGV and $period) or die <<EOM;
Usage: bigben "Hello, World!" & # Output text & time every $period minutes
       bigben -d &              # Output date & time every $period minutes
       bigben -q &              # Output a null byte every $period minutes
       bigben -u &              # Output  `uptime`   every $period minutes
       bigben -15 -d &      # Output date & time every 15 minutes
Also outputs `date` at startup and at midnight.
Program automatically terminates upon logout or network disconnection.
EOM

my $string = "@ARGV";
my $today  = 'first time';

# Sleep 1 second to start; continue while -t() is true; wake once a minute
for (sleep 1; -t(); sleep 60 - (time % 60)) {   # Stop when /dev/tty disappears

  next unless $today eq 'first time' or ((time/60) % $period) == 0;

  my ($sec,$minutes,$hours,$day,$mon,$year,$weekdaynum) = localtime;
  my $yyyymmdd = sprintf "%04d-%02d-%02d", $year+1900, $mon+1, $day;
  my $hhmmss   = sprintf "%02d:%02d:%02d", $hours, $minutes, $sec;
  my $weekday = (qw(Sun Mon Tue Wed Thu Fri Sat))[$weekdaynum];

  system 'date' unless $today eq $weekday;  # First time or at midnight

  if      ($string eq '-d') {           # Output date & time
    printf "\r\t\t\t\t  %s %s  \r\n", $yyyymmdd, $hhmmss;

  } elsif ($string eq '-q' or $string eq '-n' or $string eq '-0') {
    print "\0";                 # Quiet: output a null byte

  } elsif ($string eq '-u') {
    $_ = `uptime`; s/\s+/ /g;           # Get uptime and squeeze it
    s/^/$weekday/; s/ day(s?), /day$1_/;
    s/_(\d) min/_00:0$1/; s/_(\d\d) min/_00:$1/; s/_(\d):/_0$1:/;
    s/load average: (\S+) (\S+) (\S+)/LA:$1$2$3/;
    print  "\r\t\t  $_\r\n";            # Output modified uptime

  } else {                  # Output "string HH:MM"
    printf "\r\t\t\t\t  %s  %s %02d:%02d  %s\r\n",
        $string,$weekday,$hours,$minutes,
        ($minutes == 0 ? $yyyymmdd : '');
  }
  $today = $weekday;
}

I have my .login file run "bigben -u &" on certain hosts when it detects an SSH session.

The alternative is "ssh -o TCPKeepAlive remotehost" to keep the NAT translation active.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .