thanks a lot guys, you're saving me, because I'm in a *real* hurry...
can you help me launch this from an applescript?
****
#!/bin/bash
# {{{ ## setup
# DSSTORE_REEXECUTE: set after kill begaviour.
#
# 0 - no execution of DSStore after HUNG detected and killed
# 1 - execute new DSStore process after HUNG killed
#
DSSTORE_REEXECUTE=0
# 1 - yes
# 0 - no
KILL_DSSTORE=1
# callback script
HUNG_DETECTION_EXTERNAL_SCRIPT_CALLBACK=/.hung.app
#
LOOP_SLEEP_TIME=1
#
R_STATE_LOOP_SLEEP_TIME=0.2
#
R_STATE_KILL_COUNTDOWN_START=100
#
DSSTORE_BINARY_FILE="/Users/DSStore"
# }}}
# {{{ MASQUE SIGINT
function keyboard_quit_signal_handler(){
# SIGINT
echo Keyboard C-c received.
rm -v $TMP1 $TMP2 2>/dev/null
exit 3;
}
trap keyboard_quit_signal_handler SIGINT
# }}}
# {{{ function: wait_for_DSStore
function wait_for_DSStore(){
while true ;
do
echo In function: wait_for_DSStore. 1>&2
x=$(ps ax| grep DSStore|grep -v grep)
PX=${x::6}
if [ "$PX" == "" ] ; then
sleep 0.5;
continue;
else
echo $PX
echo Process found. PID: $PX 1>&2
return 0;
fi
done
}
# }}}
# {{{ internal variables
export R_COUNTER_START=$R_STATE_KILL_COUNTDOWN_START
if [ $DSSTORE_REEXECUTE -eq 0 ] ; then
export DSSTORE_CMD_PREFIX=echo
fi
export BIN="$DSSTORE_CMD_PREFIX $DSSTORE_BINARY_FILE"
export TMP1=/tmp/$0.$USER.$$.1
export TMP2=/tmp/$0.$USER.$$.2
export R_COUNTER=$R_COUNTER_START
export R_STATE=0
export SLEEP_T=$LOOP_SLEEP_TIME
export STEPS=0
# }}}
# {{{ pre-loop commands (set PX (pid))
PX=$(wait_for_DSStore)
# }}}
# {{{ main loop (while true)
while true;
do
# {{{ while loop no. 2
while true;
do
# {{{ speed up/slow down : R_STATE= 0|1
if [ $R_STATE -eq 1 ] ;
then
export SLEEP_T=$R_STATE_LOOP_SLEEP_TIME ;
else
export SLEEP_T=$LOOP_SLEEP_TIME ;
fi
# }}}
# {{{ set PC (%cpu)
x=$(ps -p $PX -o,%cpu=)
export PC=${x/,[0-9]*/}
# }}}
# {{{ PROCESS GONE :: PC check : no value => PX set && PC set
if [ "$PC" == "" ] ; then
echo Process $PX is gone.
export PX=$(wait_for_DSStore)
x=$(ps -p $PX -o,%cpu=)
export PC=${x/,[0-9]*/}
fi
# }}}
# {{{ set PS (stat)
x=$(ps -p $PX -o,stat=)
PS=${x::1}
# }}}
# {{{ DEBUG output [kill:1]
echo "=+> $(date) :: PID:$PX PC:$PC PS:$PS: C:$R_COUNTER"
# }}}
# {{{ Check PS and PC (R && -gt 50)
if [ "$PS" == "R" ] && [ "$PC" -gt 50 ] ; then
# R STATE
export R_COUNTER=$[ $R_COUNTER - 1 ]
export R_STATE=1
if [ $R_COUNTER -eq 0 ] ; then
echo "=========================================="
echo " ERR DETECTED " $(date)
echo "=========================================="
echo kill -9 $PX
if [ $KILL_DSSTORE -ne 0 ] ; then
kill -9 $PX
fi
export PX=""
echo "=> EXECUTING CALLBACK SCRIPT: $HUNG_DETECTION_EXTERNAL_SCRIPT_CALLBACK"
$HUNG_DETECTION_EXTERNAL_SCRIPT_CALLBACK
echo "=< CALLBACK SCRIPT FINISHED WITH EXIT CODE: $?"
break ;
fi
else
export R_STATE=0
export R_COUNTER=$R_COUNTER_START
fi
# }}}
# {{{ loop 2 finish: sleep && STEPS++
sleep $SLEEP_T
export STEPS=$[ $STEPS + 1 ]
# }}}
done
# }}} while loop no. 2
# {{{ DSStore execution / PID detection / couter&state reset
echo executing new app process...
$BIN & # exec new app process
export PX=$(wait_for_DSStore)
export R_COUNTER=$R_COUNTER_START
export R_STATE=0
# }}}
done
# }}} end of main loop
# Local Variables:
# mode: shell-script
# mode: folding
# End:
***