timeout

Linux · Processes · reference only

What it does

Runs a command and terminates it if it takes longer than the given time. It is useful to stop a process from hanging.

Syntax

timeout [opciones] duración comando [argumentos...]

Options and parameters

  • duración

    Maximum time; accepts the suffixes s, m, h and d (for example 30s or 2m).

  • -s señal

    Signal sent when time runs out; TERM by default.

  • -k duración

    If the command is still alive after that time, it kills it with KILL.

  • --preserve-status

    Returns the command's exit status even if time ran out.

  • --foreground

    Allows running it from a script without blocking on the terminal.

  • -v

    Reports the signals it sends.

Usage examples

  • timeout 5 ping -c 10 localhost

    Cuts the ping at 5 seconds even if packets were left.

  • timeout -s KILL 10 ./lento.sh

    Kills the script with KILL if it goes past 10 seconds.

  • timeout -k 5 30 ./proceso

    Asks it to close at 30 seconds and forces it 5 seconds later.

  • timeout 2m ./copia.sh; echo $?

    If time runs out, the exit status is 124.

Notes and common mistakes

  • An exit status of 124 means the command was cut by the time limit.

Related commands