#!/bin/bash # # The Bash script executes a command with a time-out. # Based on the Bash documentation example. # # Upon time-out expiration SIGTERM (15) is sent to the process. If the signal # is blocked, then the subsequent SIGKILL (9) terminates it. # Dmitry V Golovashkin (E-mail: dvg@ieee.org) # script_name="${0##*/}" # Default values. readonly param_timeout=5 readonly param_interval=1 readonly param_delay=1 declare -i timeout=param_timeout declare -i interval=param_interval declare -i delay=param_delay blue="$(tput setaf 4)" bold_red="$(tput bold; tput setaf 1)" off="$(tput sgr0)" function print_usage() { cat < 0)); do sleep $interval kill -0 $$ || exit 0 ((t -= interval)) done # Be nice, post SIGTERM first. # The 'exit 0' below will be executed if any preceeding command fails. kill -s SIGTERM $$ && kill -0 $$ || exit 0 sleep $delay kill -s SIGKILL $$ ) 2> /dev/null & exec "$@"