24

We have here a read only Bash variable. I am not allowed to unset that variable.

$ echo $TMOUT
1800

As a workaround I wrote those lines (that my session don't exit)

#!/usr/bin/perl

$|++;
while (1) { print "\e[0n"; sleep 120; }

Is there an official package (rpm) that does similar (like above Perl code) in a CentOS7/RHEL7 repository? I don't like to open up a vim editor, I wish a command.

4
  • 7
    Usual solution to this is just run cat when stepping away from your session. And then ^C it on your return.
    – steve
    Commented Aug 10, 2015 at 14:36
  • 1
    I imposed a similar rule because I had people in my team that left terminals open for weeks in a row. Whilst I do agree 1800 is kinda of low, it must be there for some reason. People tend to notice deviations to the usual...yours sessions will stand out like a sore thumb. Commented Jan 29, 2016 at 9:04
  • 1
    when it's read-only, type: exec env TMOUT=0 bash
    – nightshift
    Commented Aug 19, 2018 at 3:39
  • I've found that simply not closing the terminal on exit works around most of the reasons I hate this "feature". Trying to remember to cat is just setting yourself up to fail. Commented Jan 12, 2023 at 20:23

6 Answers 6

23

Add this to the start of your .bash_profile ?

if [ ! -z "$TMOUT" ]; then
  env -i bash --init-file ~/.bash_profile
fi

Beware the wrath of the sysadmins if you leave a gazillion old sessions running as a result of defeating their timeout rulings.

4
  • 1
    This is truly EVIL!
    – Ahi Tuna
    Commented Dec 20, 2019 at 15:47
  • 7
    The -i argument wipes out the environment. You can be more specific and only unset a single variable like so: env -u TMOUT bash --init-file ~/.bash_profile Commented Mar 18, 2020 at 9:59
  • 1
    I use --norc to ignore the system settings.
    – Polluks
    Commented Jul 25, 2022 at 12:02
  • I prefer the -u TMOUT version because the -i version also unsets $USER.
    – IpsRich
    Commented Jul 8 at 8:29
10

Here is the thing,
When the session variable is "Read Only" you have to replace the current shell process with the command by "exec"
So, the Answer to your question is:

$> exec env TMOUT=0 bash

But I recommend setting a higher timeout value

$> exec env TMOUT=3600 bash
2
  • 1
    If I add exec env TMOUT=0 bash to .bashrc, is it going to work in the same way?
    – notilas
    Commented Nov 15, 2020 at 5:41
  • I added exec env TMOUT=86400 bash to .bashrc and when I tried to open a new putty session, it took a long time to start the shell. Then I moved that line to .bash_profile and it's working fine.
    – arun
    Commented Apr 5 at 17:09
7

You can issue perl commands from the command line...

perl -e '$|++; while (1) { print "\e[0n"; sleep 120; }'

or you could do the same in shell (a sh/bash example):

while sleep 120; do printf '\33[0n'; done

Or you could use watch:

watch -n 120 printf '\33[0n'

2
  • what is printf '\33[0n'? i know what the command line printf is. but what is being printed? Commented Mar 13, 2018 at 14:35
  • 1
    \33 is shorthand for the escape key <esc>[0n is not a known ANSI escape code, I imagine it was a typo and should of been <esc>[0m which resets the terminal colour for apps/terminals/tty's that support ANSI escape codes.
    – Drav Sloan
    Commented Mar 14, 2018 at 18:01
4

I my case, it is defined in /etc/profile.d/bash_autologout.sh:

TMOUT=1800
readonly TMOUT
export TMOUT

To find it, I did:

sudo find /etc/ -name "*" | xargs sudo grep "TMOUT" 2>&1 | grep -v "filter..."
1
  • I have /etc/bash.bashrc
    – Polluks
    Commented Jul 4, 2022 at 12:39
0

export TMOUT=0

top

You will have some activity and your session will stay and you could see how long it runs.

-1

Why aren't you switching to non-interactive session?

# TMOUT=0
-bash: TMOUT: readonly variable
# unset TMOUT
-bash: unset: TMOUT: cannot unset: readonly variable
# su
# export TMOUT=10
# unset TMOUT
# 
1
  • I'm not a superuser.
    – Polluks
    Commented Jul 4, 2022 at 12:36

You must log in to answer this question.

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