Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # !/bin/sh
- # Timeout wrapper
- # Get a pid of background process
- # s.sh contents:
- # echo "before"
- # sleep <60s или >60s, для тестов
- # echo "after"
- # exit 150
- exec ./s.sh &>${BASHPID}.out &
- pid=$!
- # Timeout limit (minutes in this example)
- $maxmin=1
- sec=0
- while [ 1 ]; do
- let "sec = $sec + 1"
- # see pid exists in /proc/
- if [[ ! -d "/proc/$pid" ]] || [[ `find /proc/$pid -type d -cmin +$maxmin` ]]; then
- if [[ -d "/proc/$pid" ]]; then
- kill $pid
- echo "Timeout! More than $maxmin minutes! Kill the process. ($sec s)"
- else
- wait $pid
- echo "Process completed normally. Exit code is $? ($sec s)"
- fi
- echo "Timeout or terminated! ($sec s)"
- # remove out file
- rm $pid.out 2>/dev/null
- break
- else
- sleep 1s
- fi
- done
Add Comment
Please, Sign In to add comment