Search

Automatically kill scripts and commands after a certain duration

When running scripts, we might want to restrict the time for which a script runs. May be printing out the logs for 10 minutes and then stop automatically or stopping a script if it does not exit by itself. We can use the command time out for this .

Timeout command runs a command for a specific duration and kills the command after that duration if it has not exited by itself.

syntax:



Let us take the following script,test.sh, as an example



The script sleeps for 15 seconds and then prints the message done.

If we run the script with timeout of 5 seconds the message "done" will not get printed as the script will get killed before 15 seconds as shown below.



Running the script with out the timeout, we will see the message done being printed.