Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: silently kill background function process

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?

like image 688
rouble Avatar asked Apr 19 '11 15:04

rouble


People also ask

How do you kill a background process?

Killing a background process is fairly straightforward; use the command pkill and the process ID, or process name as: Using the pkill command will force terminate (-9) the processes with the process name of ping.

Can we kill any background executing process?

pkill. pkill is one of the commands that can directly kill a background process, using the process name.

How do you kill a running background in Linux?

To cancel a background job, use the kill command. To be able to kill a process, you must own it. (The superuser, however, can kill any process except init.) Before you can cancel a background job, you need to know either a PID, job identifier, or PGID.


2 Answers

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

like image 72
Mark Edgar Avatar answered Sep 20 '22 18:09

Mark Edgar


Just came across this myself, and realised "disown" is what we are looking for.

foo & foo_pid=$! disown  boring_and_long_command kill $foo_pid sleep 10 

The death message is being printed because the process is still in the shells list of watched "jobs". The disown command will remove the most recently spawned process from this list so that no debug message will be generated when it is killed, even with SIGKILL (-9).

like image 20
pix Avatar answered Sep 23 '22 18:09

pix