Welcome to the next pikoTutorial!
Bash comes with a bunch of useful, built-in commands. One of them is timeout which allows you to run another command with a time limit. The syntax is simple:
timeout [duration] [target_command]To avoid providing large numbers for long timeouts, duration parameter accepts suffixes which mark the exact time frame:
s– secondsm– minutesh– hoursd– days
Simple example
For the first example, let’s take a simple Python script which spins forever:
# some_job.py
import time
while True:
time.sleep(1)We can run it with a 3 seconds timeout using the following command:
timeout 3s python3 some_job.pyAfter 3 seconds of waiting, some_job.py is terminated.
Specifying termination signal
When the allowed time elapses, timeout sends SIGTERM signal to the given command. In your implementation you may have some custom signal handlers which do the cleanup before exiting. For example, if you expect that your script will be interrupted mainly by CTRL + C, you most probably have a SIGINT signal handler. In such case, you most likely want to keep this in case of timeout command as well to still be able to perform the cleanup when the timeout occurs:
# some_job.py
import sys
import signal
import time
def handle_sigint(signum, frame):
print("Received SIGINT, cleaning up and exiting...")
sys.exit(0)
signal.signal(signal.SIGINT, handle_sigint)
print("Starting script...")
time.sleep(10)If you now call:
timeout 3s python3 some_job.pyYou will see that your signal handler has not been called before exiting the script. To change this, specify the expected signal using --signal option:
timeout --signal=SIGINT 3s python3 some_job.pyAfter that, the log from handle_sigint handler is visible when the timeout occurred.
Giving time for graceful exit
In the previous example I assumed that the cleanup is quick and always succeeds, but in reality the application shutdown may be time consuming:
# some_job.py
import sys
import signal
import time
def handle_sigterm(signum, frame):
print("Received SIGTERM, cleaning up and exiting...")
time.sleep(5)
signal.signal(signal.SIGTERM, handle_sigterm)
print("Starting script...")
time.sleep(10)In such case, it may require its own timeout for wrapping things up – upon violation of that final timeout, the application should be killed. This can be achieved by using --kill-after. The following command lets the script run for 5 seconds, sends SIGTERM (default signal) and then it gives the script 2 more seconds to exit. If the script doesn’t exit within 2 seconds after receiving SIGTERM, SIGKILL is sent:
timeout --kill-after=2s 5s python3 some_job.pyPreserving return code
By default, if the command provided to timeout times out, its return code will be equal to 124. You can check it by running the example Python script and then checking its return code:
timeout 3s python3 some_job.py
echo $?Note for beginners:
?in the above section is a special Shell variable which stores the exit code of the last executed command.$sign, as usually in Shell environment, obtains the underlying value of that variable, soecho $?prints the exit status of the most recent command.
However, this may be something that your system does not expect, especially if your script has some logic which ends up returning specific exit codes. For example, the following script simulates a time consuming processing of 3 different phases. In case of interruption, the script returns the number of the phase being processed during the interruption:
# some_job.py
import sys
import signal
import time
current_phase: int = 0
def handle_sigterm(signum, frame):
sys.exit(current_phase)
signal.signal(signal.SIGTERM, handle_sigterm)
for i in range(3):
current_phase += 1
time.sleep(2)To preserve that return code after timeout, use --preserve-status flag:
timeout --preserve-status 3s python3 some_job.pyNow, when you check the exit status of such operation, you will see 2 because timeout terminated script while the phase 2 was being processed.
Read also:
- A 40-line LLM-based bash command executor in Python
- GTest and short-circuit evaluation in C++
- AI is powerful. Snippets are instant.
- From AUTOSAR to S-Core: the first C++ pub/sub implementation
- How to write Arduino Uno code with Python?
- Combining Bazel with Docker
- Running commands with timeout on Linux
- Running Python unit tests with CMake
- Thirdparty dependencies with FetchContent
- Bug of the week #11
- Combining CMake with Docker
- How to search the internet from Linux terminal?
- Folding expressions in C++
- How to derive from an enum in Python?
- Bug of the week #10
- Trying ROS2: client/server within a single container
- Make C++ a better place #4: Go as an alternative
- How to convert hex to dec in Linux terminal?
- Setting up a Python project with CMake
- Separating builds for different configs with Bazel
- Trying ROS2: pub/sub within a single container
- Bug of the week #9
- UDP multicasting with Python
- Destruction order vs thread safety in C++
- Let’s review some code: C++ #2
- Make C++ a better place #3: D as an alternative
- Registering callback using std::function in C++
- Bug of the week #8
- TCP client/server with Python
- Simple menus in Bash scripts with select
- Calling member function on a nullptr in C++
- Bug of the week #7
- Python lru_cache explained
- How to dockerize a Python application?
- Make C++ a better place #2: CppFront as an alternative
- Parameters combinations in GoogleTest
- Data transfer with curl
- Python reduce explained
- Bug of the week #6
- Custom literals in C++
- Linux and hash command
- 5 Python good practices which make life easier
- Let’s review some code: Python #1
- Make C++ a better place #1: What does better mean
- Enums vs enum class in C++
- Bug of the week #5
- UDP client/server with Python
- Hard links in Linux
- Functions calling order in unit tests in C++
- Bug of the week #4
- Yield in Python – state machines, coroutines and more
- Copy files from another branch with Git
- Make C++ a better place #0: Introduction
- 5 misconceptions about std::move in C++
- How to use xargs on Linux?
- How to test method call order with unittest in Python?
- Bug of the week #3
- Build & run C++ unit tests with CMake
- Arrange text with sort on Linux
- Key derivation function with Python









