shell scripting, how to auto-timeout?

Nerius Landys nlandys at gmail.com
Fri Jan 23 10:58:12 PST 2009


> #!/bin/sh -T
>
> kill_all()
> {
>        echo 'killing everything'
>        kill $SPID $CPID 2> /dev/null
>        exit 0
> }
>
> trap kill_all SIGCHLD
>
> ./child &
> CPID=$!
>
> sleep 5 &
> SPID=$!
>
> echo "child is $CPID"
> echo "sleeper is $SPID"
> wait

This is very nice.  However I'm getting one problem still.  My script
prints a "Terminated" to standard out, and this is bad because the
purpose of this java program is to print to standard out, so the
output gets jumbled.  The script I have is pasted below, and the
"Terminated" string seems to be printed out from the kill command that
kills the sleep thread.

#!/bin/sh -T

cd `dirname "$0"`
CLASSPATH="mapgen.jar"
export CLASSPATH

kill_all()
{
    kill "$JAVA_PID" > /dev/null 2>&1
    JAVA_KILL_EXIT_STATUS="$?"
    EXIT_STATUS=0
    if [ "$JAVA_KILL_EXIT_STATUS" -eq 0 ]; then
        echo "Terminated infinite looping in Java process." 1>&2
        EXIT_STATUS=1
    fi
    kill "$SLEEP_PID" > /dev/null 2>&1
    exit "$EXIT_STATUS"
}

trap kill_all SIGCHLD

/usr/local/bin/java PipeGenerator $* &
JAVA_PID="$!"

sleep 3 &
SLEEP_PID="$!"

wait


More information about the freebsd-questions mailing list