Timeout for Linux process – Killing or cleanly closing a process in Linux based on timeout / time limit

Timeout on a script or program in Linux is easy!
Did you ever have the need to kill a process if it takes too long to run for any reason? Then this post will show you how you do just that.

This could be used to prevent a cronjob for running too long or prevent cronjobs from overlapping if a lock-file is not the optimal solution in your case.
It could also be used in a case where a process freezes for unknown reasons, and you need to force a restart of it based on a how long it has been running for.

This is not just useful for cronjobs, it can set a timeout on any process, script or program you run!

Not only that, you can even tell the process to exit in a clean way, or force it. Whichever fits your needs. The timeout can send any type of kill signal to the process you are monitoring to close it when the timeout is reached.
It can also return the exitcode of the script/program/job that timed out which makes it useful in scripts if you need to act on the way the script or program ends.

What you’ll need

You won’t need much for this, it will most likely already be on your linux system
There is a binary called “/usr/bin/timeout”, which is part of the “coreutils” package. If you do not have this package already it can be installed using the following commands:

apt install coreutils

yum install coreutils

Configuration parameters

First a little about the configuration parameters of the timeout binary, as it has a few that you might want depending on what you are trying to set a timeout on.

You might not need this and it’s optional. It depends on what you are trying to do. This parameter just makes the timeout command exit with the same exitcode as the process it was monitoring
Even if the timeout is hit, it will exit with the same exitcode as the process that timed out

–kill-after=DURATION, -k DURATION
The amount of seconds to wait before closing the monitored process
You can even specify a suffix to the duration amount here, “s” for seconds, “m” for minutes”, “h” for hours and “d” for days.
Example: –kill-after=20m

–signal=SIGNAL , -s SIGNAL
The kill signal you want to send to the monitored process. This can be “9” or “SIGKILL” to kill it forcefully
“15” or “SIGTERM” is the normal/clean/soft way to kill a process by telling it to clean it’s resources and exit when ready – This will not work if the process is frozen or in any other way not responding
You can view all the available kill signals/levels by typing the command “kill -L”, output:

1 HUP      2 INT      3 QUIT     4 ILL      5 TRAP     6 ABRT     7 BUS

8 FPE      9 KILL    10 USR1    11 SEGV    12 USR2    13 PIPE    14 ALRM

15 TERM    16 STKFLT  17 CHLD    18 CONT    19 STOP    20 TSTP    21 TTIN

22 TTOU    23 URG     24 XCPU    25 XFSZ    26 VTALRM  27 PROF    28 WINCH

29 POLL    30 PWR     31 SYS

Quick syntax/usage for the Linux timeout command

The easiest/fastest way to use this, is with the following syntax:
timeout -s <signal> <kill after time> <command>

To run the sleep command for 11 minutes (660 seconds) and close the sleep command cleanly with kill signal 15 when it has been running for only 10 minutes, the command would looks like this:
timeout -s 15 10m sleep 660

That’s all! You can append any command to timeout you want, i just used “sleep 660” to test

The entire man/help page for the timeout command can be found here or by typing “timeout –help” in a terminal

Leave a Reply

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