bash: silently kill background function process

rouble picture rouble · Apr 19, 2011 · Viewed 36.3k times · Source

shell gurus,

I have a bash shell script, in which I launch a background function, say foo(), to display a progress bar for a boring and long command:

foo()
{
    while [ 1 ]
    do
        #massively cool progress bar display code
        sleep 1
    done
}

foo &
foo_pid=$!

boring_and_long_command
kill $foo_pid >/dev/null 2>&1
sleep 10

now, when foo dies, I see the following text:

/home/user/script: line XXX: 30290 Killed                  foo

This totally destroys the awesomeness of my, otherwise massively cool, progress bar display.

How do I get rid of this message?

Answer

Mark Edgar picture Mark Edgar · Apr 19, 2011
kill $foo_pid
wait $foo_pid 2>/dev/null

BTW, I don't know about your massively cool progress bar, but have you seen Pipe Viewer (pv)? http://www.ivarch.com/programs/pv.shtml