If you want to ensure a command does not take too much time to execute, you can add a time limit after which the command will be killed.

Table of Contents

Installation

timeout is part of coreutils, so it’s probably already installed on your computer.

Usage

This command is pretty simple: type timeout, a time limit and the target command:

timeout <DURATION> <COMMAND>
  • <DURATION>: a floating point number with an optional suffix: s (seconds, the default), m (minutes), h (hours) or d (days).

As an example, to limit the duration of a python script to 2 minutes:

timeout 2m python3 myscript.py

After 2 minutes, the target command (python3 myscript.py) will be killed (TERM signal). To change the kill signal, use -s=<SIGNAL>. <SIGNAL> can be a name or a number. Run kill -l for a list of signals.

If you have any suggestion, feel free to contact me via social media or email.