TMOUT – Automatically Exit Unix Shell When there is No Activity

by SathiyaMoorthy on May 7, 2010

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.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

{ 5 comments… read them below or add one }

1 Alan MeC Lacerda May 7, 2010 at 5:56 am

Very nice tip!

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

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

3 Ramesh Natarajan May 7, 2010 at 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.”

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

Sorry, you’re right.

5 Kiran May 8, 2010 at 11:35 am

Cool tip.

Thx
Kiran

Leave a Comment

Previous post:

Next post: