Miracle of shell programming "shell timeout" 
Thursday, August 20, 2009, 08:15 PM
I needed shell script to execute specific action on boot of some virtual appliance
script bellow do that function exactly...
I hope script is self explanatory

(Click on Citaj dalje... for script)


#!/bin/sh
#
# settings
EXECUTE_ON_TIMEOUT=/usr/bin/python
EXECUTE_ON_ENTER=/bin/date
export TIMEOUT=10
#
# shell pid...
export PID=$$
#
wait4enter() {
echo "Loading/Starting ${START_ON_TIMEOUT}"
# do some stuff here or what ever you like...
# exec is used to replace currently running shell so nothing is executed after trap
exec ${EXECUTE_ON_TIMEOUT}
exit # not really needed just looks better :)
}
#
exitonsigint() {
echo "# EEEEEK I'M KILLED"
kill -s TERM $TIMERPID
exit # stop execution of this script
}
#
# spawn process which will wait for decision or timeout
(sleep $TIMEOUT ; kill -s USR2 $PID) &
# get pid...
TIMERPID=$!
#
# trap signal from timeout... or sigint (CTRL+C)
trap wait4enter USR2
trap exitonsigint INT
#
echo "# Press ENTER to start ${EXECUTE_ON_ENTER}"
echo "# or wait ${TIMEOUT} sec to load ${EXECUTE_ON_TIMEOUT}"
#
# enter key is enough
read nothing
kill -s TERM ${TIMERPID}
exec ${EXECUTE_ON_ENTER}

Komentari

Dodaj komentar
Zao nam je. Novi komentari nisu dozvoljeni posle 180 dana.