How to use the Linux timeout command

If you tend to issue commands and accidentally leave them running, you might want to employ the timeout command

Linux admins are notorious for depending on the command line. With good reason. The command line is incredibly powerful. There is no end to what you can do with Linux commands.

However, there are times when you want to run a command but don't want the command to continue running until you forget it's running and realize that the command has been gobbling up CPU cycles, filling up logs, or just generally doing its thing in the background, harming nothing.

SEE: Disaster recovery and business continuity plan (Tech Pro Research)

How it works

Regardless of why you don't want to allow a command to run forever, the how is quite simple--thanks to the timeout command. The timeout command should be installed by default and is very simple to use. Say you want to run a ping command, on, for five seconds (because who hasn't forgotten they'd run a ping command, only to come back hours later to see it still pinging the target address?).

To do this, log into your Ubuntu server or desktop, open a terminal window, and issue the command timeout 5 ping The ping command will do its thing for five seconds and stop. Or say you want to follow the syslog log file with tail for ten seconds. That command would be timeout 10 tail -f /var/log/syslog. After the configured 10 seconds, the tail command will end.

And that's how you can automatically stop your commands, without having to resort to the old [Ctrl]+[C] keyboard combination. If you tend to issue commands and accidentally leave them running, you might want to start employing the timeout command, before your IT manager puts you in a timeout.

Also see

Image: Jack Wallen