tuto:linux:script_timeout_en_shell
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
tuto:linux:script_timeout_en_shell [2010/01/22 21:44] – créée root | tuto:linux:script_timeout_en_shell [2013/03/12 22:13] (Version actuelle) – root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Timeout en SHELL ====== | ====== Timeout en SHELL ====== | ||
- | < | + | **timeout.sh** |
- | # | + | < |
- | + | #!/bin/sh | |
- | timeout=$1 | + | |
- | command=$2 | + | # Execute a command with a timeout |
- | shift 2 | + | |
- | + | # Author: | |
- | check_pid_name() { | + | # http:// |
- | | + | # Notes: |
- | | + | # If the timeout occurs the exit status is 124. |
- | [ -d /proc/$childpid ] || return | + | # There is an asynchronous (and buggy) equivalent of this |
- | [ $(grep -c $command | + | # script packaged with bash (under / |
+ | # which I only noticed after writing this. | ||
+ | # Note there is a timeout | ||
+ | # I noticed later again that there is a C equivalent of this packaged | ||
+ | # with satan by Wietse Venema, and copied to forensics by Dan Farmer. | ||
+ | # Changes: | ||
+ | # V1.0, Nov 3 2006, Initial release | ||
+ | # V1.1, Nov 20 2007, Brad Greenlee < | ||
+ | # Make more portable by using the ' | ||
+ | # | ||
+ | # V1.3, Oct 29 2009, Ján SárenÃk < | ||
+ | # Even though this runs under dash,ksh etc. | ||
+ | # it doesn' | ||
+ | # Also change exit on timeout from 128 to 124 | ||
+ | # to match coreutils. | ||
+ | # V2.0, Oct 30 2009, Ján SárenÃk < | ||
+ | # | ||
+ | # | ||
+ | |||
+ | if [ "$#" -lt " | ||
+ | echo " | ||
+ | echo " | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | cleanup() | ||
+ | { | ||
+ | trap - ALRM # | ||
+ | kill -ALRM $a 2>/dev/null #stop timer subshell if running | ||
+ | kill $! 2>/ | ||
+ | exit 124 #exit with 124 if it was running | ||
} | } | ||
- | + | ||
- | [ -z " | + | watchit() |
- | + | { | |
- | safe_run() { | + | trap " |
- | | + | sleep $1& wait |
- | | + | kill -ALRM $$ |
- | $command $* | + | |
- | | + | |
} | } | ||
- | + | ||
- | safe_run | + | watchit |
- | childpid=$! | + | shift # |
- | sleep $timeout | + | trap " |
- | + | ("$@" || exit $?)& wait $!; RET=$? | |
- | check_pid_name | + | kill -ALRM $a #send ALRM signal to watchit |
- | kill $childpid | + | wait $a #wait for watchit to finish cleanup |
- | sleep 0.1 | + | exit $RET # |
- | | + | |
- | echo "$command $* timed out" | + | </ |
+ | |||
+ | ./ | ||
+ | |||
+ | Un autre script s' | ||
+ | <code bash> | ||
+ | # | ||
+ | |||
+ | dir_result="/ | ||
+ | |||
+ | declare | ||
+ | |||
+ | usage() { | ||
+ | echo "Usage : $0 < | ||
+ | exit 1 | ||
} | } | ||
+ | |||
+ | kill_proc() { | ||
+ | for pid in ${command_pid[*]}; | ||
+ | [ -d /proc/$pid ] && [ `grep $0 / | ||
+ | done | ||
+ | } | ||
+ | |||
+ | while getopts :h OPTION | ||
+ | do | ||
+ | case " | ||
+ | h) | ||
+ | usage | ||
+ | ;; | ||
+ | esac | ||
+ | done | ||
+ | |||
+ | |||
+ | nb_arg=`echo " | ||
+ | |||
+ | [ ! -d $dir_result ] && mkdir -p " | ||
+ | |||
+ | echo "Table des processus :" | ||
+ | |||
+ | for ((i=1; i< | ||
+ | do | ||
+ | log=`expr $i - 1` | ||
+ | cmd=`echo " | ||
+ | |||
+ | command[${# | ||
+ | |||
+ | if [ `echo " | ||
+ | timeout=`echo " | ||
+ | fi | ||
+ | |||
+ | if [ -z $timeout ] ; then | ||
+ | ( | ||
+ | $cmd 2>&1 1> | ||
+ | ret=$? | ||
+ | sleep 0.1 | ||
+ | echo "$! : Fait ($ret)" | ||
+ | [ $ret -ne 0 ] && exit 1 | ||
+ | ) & | ||
+ | else | ||
+ | ( | ||
+ | `dirname $0`/ | ||
+ | ret=$? | ||
+ | sleep 0.1 | ||
+ | if [ $ret -eq 0 ] ; then | ||
+ | echo "$! : Fait ($ret)" | ||
+ | else if [ $ret -eq 124 ] ; then | ||
+ | echo "$! : Timeout" | ||
+ | exit 1 | ||
+ | else | ||
+ | echo "$! : Fait($ret) !" | ||
+ | exit 1 | ||
+ | fi | ||
+ | fi | ||
+ | ) & | ||
+ | |||
+ | unset timeout | ||
+ | fi | ||
+ | |||
+ | command_pid[${# | ||
+ | done | ||
+ | |||
+ | for i in ${!command[*]} | ||
+ | do | ||
+ | if [ `echo ${command[i]} | awk '{ print $1 }' | grep " | ||
+ | echo " | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | done | ||
+ | |||
+ | echo "" | ||
+ | |||
+ | trap ' | ||
+ | |||
+ | wait | ||
+ | |||
+ | for i in ${!command[*]} | ||
+ | do | ||
+ | [ -f " | ||
+ | done | ||
</ | </ | ||
- | ./script 2 sleep 4 | + |
tuto/linux/script_timeout_en_shell.txt · Dernière modification : 2013/03/12 22:13 de root