tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

killing subshells in /bin/sh scripts

I have the following script:

    # cat
    echo "start script: $$"
    ( sleep 3 && echo "3s have elapsed" ) &
    echo "$pid started in background"
    kill_job() {
	echo "killing $pid"
	kill "$pid"
    trap "kill_job" 1 2 3 15
    echo "end script"

If I run this with NetBSD-7.1 /bin/sh, I get:

    # /bin/sh
    start script: 11904
    29622 started in background
    3s have elapsed
    end script

If I interrupt this script with Ctrl-C within 3s, I get:

    # /bin/sh
    start script: 17936
    28641 started in background
    ^Ckilling 28641
    end script
    # 3s have elapsed   <-- output after "sleep 3" finishes

Why does the output still appear after the script has ended?
If I run this with /bin/ksh, I don't get any output after the
end of the script.

Johnny C. Lam

Home | Main Index | Thread Index | Old Index