linux setup program run timeout

In some cases, we need to limit the running time of the program (such as cronjob, etc.), here briefly under implementation and use of the timeout signal

1. If the following codes (test_timout.sh):

#!/bin/bash

while true
    do
    echo -n "Current date: "
    date
    sleep 1
done

Once run (bash test_timout.sh), can not be self-terminating; if there is a bug in the code, causing the program can not be terminated properly, the machine's resources are not released (if the cronjob is the case, it will take up more and more resources many),

Thus in this case, we need to set the run time of the program; and a timeout command signal is achieved by the following

2. Let the above code after 3 seconds to exit the field of solution is as follows:

   1) Modify the above code (test_timout.sh), so that it can then exit capture signal

#!/bin/bash

trap "echo received a interrupt signal; exit 1" SIGINT

while true
    do
    echo -n "Current date: "
    date
    sleep 1
done

  2) Run the name bash test_timout.sh to timeout. 3 -s SIGINT bash test_timout.sh ; so that after three seconds, the program will automatically exit

Guess you like

Origin www.cnblogs.com/276815076/p/10966751.html
Recommended