We're an ISO27001:2013 Certified Supplier


When running commands by hand it’s often easy to tell when something is taking too long to run, and then to cancel it with Ctrl+C or similar. But what if you need to run something and leave it unattended, for example a cron job?

It turns out the timeout command can help with that, and it’s very simple to use:

timeout 30s a-very-slow-command

With the above example, the slow command will be terminated (sent a SIGTERM signal) if it’s still running after 30 seconds. You can use other suffixes (such as m for minutes, h for hours and d for days) and use floating point numbers (e.g. 1.25m is 75 seconds).

If you have to deal with a particularly difficult command and a SIGTERM isn’t enough you can instruct timeout to send a SIGKILL after an additional delay:

timeout -k 20s 1.5m badly-behaved-command

In this example, badly-behaved-command will be told to terminate after 1.5 minutes, and 20 seconds after that it will be killed.

timeout is part of GNU coreutils and should be available out of the box on most Linux systems. Further command-line options are available; be sure to check the timeout(1) man page to get an idea of what it can do. If you need additional features an alternative called timelimit is available in most distributions, see timelimit(1) for what it does differently.

Photo by Aron on Unsplash

Leave a Reply

Your email address will not be published. Required fields are marked *

Get free Linux business strategies

Fill in this form and we'll send you updated Linux business strategies and ideas each week
  • This field is for validation purposes and should be left unchanged.