Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 23 Jan 2009 10:58:11 -0800
From:      Nerius Landys <nlandys@gmail.com>
To:        Maxim Khitrov <mkhitrov@gmail.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: shell scripting, how to auto-timeout?
Message-ID:  <560f92640901231058i3c9e8645n6a30c74ec868ba87@mail.gmail.com>
In-Reply-To: <26ddd1750901221635k17230c7eudb1edc38c808eb83@mail.gmail.com>
References:  <560f92640901221241y4fc1620aree083a812c1f3c8d@mail.gmail.com> <26ddd1750901221333x5356f4f3l6b6410fc05d4e6d4@mail.gmail.com> <560f92640901221451j2e2b259bw1559a8c8d8912941@mail.gmail.com> <560f92640901221458y9409360n34904461fb2580e4@mail.gmail.com> <26ddd1750901221635k17230c7eudb1edc38c808eb83@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> #!/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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?560f92640901231058i3c9e8645n6a30c74ec868ba87>