Outils pour utilisateurs

Outils du site


tuto:linux:script_timeout_en_shell

Ceci est une ancienne révision du document !


Timeout en SHELL

#!/bin/bash
 
timeout=$1
command=$2
shift 2
 
check_pid_name() {
        command=$1
        childpid=$2
        [ -d /proc/$childpid ] || return 1
        [ $(grep -c $command /proc/$childpid/cmdline 2>/dev/null) -gt 0 ] && return 0 || return 1
}
 
[ -z "$command" ] && echo "Usage: safecmd   [args]">&2 && exit 1
 
safe_run() {
        command=$1
        shift
        $command $*
        kill $$
}
 
safe_run $command $* &
childpid=$!
sleep $timeout
 
check_pid_name $command $childpid && {
        kill $childpid
        sleep 0.1
        check_pid_name $command $childpid && kill -9 $childpid
        echo "$command $* timed out"
}
./script 2 sleep 4
tuto/linux/script_timeout_en_shell.1264196692.txt.gz · Dernière modification : 2010/01/22 21:44 de root