≡ Menu

TMOUT – Automatically Exit Unix Shell When there is No Activity

Question: I would like to terminate my Unix command line shell, when I don’t execute any command for N number of seconds. i.e How to automatically log out if there is no activity in a Linux shell ?

Answer: TMOUT variable in bash will terminate the shell if there is no activity for N seconds as explained below.

# export TMOUT=N
  • N is in seconds. When there is no activity for N seconds, the shell will be terminated.

Example: Terminate the shell if there is no activity for 5 minutes.

# export TMOUT=300

If there is no activity in a particular shell for more than 5 minutes, then it will terminate that shell. You cannot use this technique to logout from a GUI session.

From man bash:

       TMOUT  If  set  to  a  value greater than zero, TMOUT is treated as the
              default timeout for the read builtin.  The select command termi‐
              nates if input does not arrive after TMOUT seconds when input is
              coming from a terminal.  In an interactive shell, the  value  is
              interpreted  as  the  number  of seconds to wait for input after
              issuing the primary prompt.  Bash terminates after  waiting  for
              that number of seconds if input does not arrive.

TMOUT is useful when you are ssh-ing to a remote server and would like to log out from the remote server when you don’t perform any activity for x number of seconds. Add the export command to your .bash_profile or .bashrc on the remote-server.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Alan MeC Lacerda May 7, 2010, 5:56 am

    Very nice tip!

  • Chris F.A. Johnson May 7, 2010, 6:50 am

    That will terminate a script that is waiting for input to a read command; it will not terminate an interactive shell.

  • Ramesh Natarajan May 7, 2010, 9:12 am

    @Chris,

    It did terminate the interactive shell for me when I tried it.

    Also from the bash man page:
    “In an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the primary prompt.”

  • Chris F.A. Johnson May 7, 2010, 9:34 am

    Sorry, you’re right.

  • Kiran May 8, 2010, 11:35 am

    Cool tip.

    Thx
    Kiran

  • Jason Bacon May 7, 2014, 8:32 am

    Actually, it’s not necessary to export TMOUT. It’s a shell variable, not an environment variable, so

    TMOUT=300

    would be sufficient.