Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash : If command timeout, execute something else

Tags:

bash

timeout

I am doing the following :

timeout 180 bash myscript.txt

myscript.txt is supposed to be fully executed in less than 180 seconds. IF NOT, I want emergencyscript.txt to be executed. Is it possible to do so ?

Like

timeout 180 [bash myscript.txt] [bash emergencyscript.txt]
like image 860
Vincent Avatar asked Oct 22 '25 13:10

Vincent


2 Answers

The exit status of timeout is 124 if the command times out. You can test for this explicitly.

timeout 180 bash myscript.txt
exit_status=$?
if [[ $exit_status -eq 124 ]]; then
    bash emergencyscript.txt
fi

Any other value for exit_status is the result of myscript.txt running to completion.

like image 170
chepner Avatar answered Oct 24 '25 03:10

chepner


#!/bin/bash
if ! timeout 180s bash myscript.txt; then bash emergencyscript.txt; fi
like image 25
fflores Avatar answered Oct 24 '25 02:10

fflores