Kill background process, but no display

Hi all.

Consider this simple script
------------
#!/bin/sh

# silent background program
background_prog > /dev/null 2>&1 &
PID1=$!

# do something ...
echo LA LA LA

# kill the background program
kill $PID1

# continue
echo LI LI LI
---------------------

My problem is, the display become like this
LA LA LA
3236: Terminated
LI LI LI

I simply want the "XXXX: Terminated" message is not displayed.
How ?

Thanks.
LVL 5
KocilAsked:
Who is Participating?
 
alikoankCommented:
if you do not want to disable output completely you can put this statments in paranthesis.

(
   exec 2>/dev/null
   kill $PID
)

bash executes the statements in paranthesis in a separate shell, basically same as spawning a new /bin/bash. That way you will not have to re-enable error output.

Again: I'm nowhere near a unix box now, so I have not tried it.
0
 
sunnycoderCommented:
Hi Kocil,

> kill $PID1
kill $PID1 2>&1 >/dev/null

Sunnycoder
0
 
da99rmdCommented:
Hi Kocil,
its the shell that echo this message, so it desnt help using the > /dev/null 2>&1 on kill either.
But ill look in to tuning the bash to not echoing this message ill get back to you.

/Rob
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
da99rmdCommented:
sunnycoder,
Does it work ?
If yes then kill must be a background process it self because some times the message comes a long time after the killing occurs.
/Rob
0
 
KocilAuthor Commented:
That does not work Sunny.
0
 
pYraniaCommented:
Isn't it

> kill $PID1
kill $PID1 >/dev/null

? (Catch every output)
0
 
da99rmdCommented:
pYrania,
it works like this :
> /dev/null catches only stdout
2> /dev/null catches stderr and
2>&1 >/dev/null catches stderr but before that it maps the file descriptor for stdout to stderr.

So the last one catches all the output but this isn’t the problem here I guess.
The problem is to make the shell not to output that a job has been finnished or terminated.

/Rob
0
 
pYraniaCommented:
ah alright.

so this still doesn't catch the bash notice...

maybe the following will do (no shell to test right now)

unset notify
kill $PID1 2>&1 >/dev/null
set notify
0
 
da99rmdCommented:
pYrania,
I couldent make it work it outputed the **** anyway :(.

/Rob
0
 
hvd24Commented:
Hi ,
   Just try it
   kill $PID1 > /dev/zero.
Thats it.
Hari.D
0
 
KocilAuthor Commented:
No. It does not too hvd24.
---------------
root:# cat /dev/ttyS0 &     
[1] 895
root:# PID=$!              
root:# kill $PID > /dev/zero
root:#
[1]+  Terminated              cat /dev/ttyS0
root:#
---------------

Using kill $PID > /dev/null, the message comes out immediatelly.
kill $PID > /dev/zero, the message comes out after you press enter.


0
 
KocilAuthor Commented:
The "unset notify" does not work too.

Man, this is harder than I though.
0
 
sunnycoderCommented:
My bash does not complain anything ... It cries only if no process is killed (pid not found) but otherwise  for normal operation it behaves well

GNU bash, version 2.05b.0(1)
0
 
da99rmdCommented:
sunnycoder,
In what case the pipe or the unset ?
my bash complains or gives me the output not all the time with bash version 2.05.8(1)-release
and never with bash version 2.05b.0(1) :)
this is the script i ran:
# silent background program
sleep 10 > /dev/null 2>&1 &
PID1=$!

# do something ...
echo LA LA LA

# kill the background program
kill $PID1

# continue
echo LI LI LI

on both bash versions.

/Rob
0
 
sunnycoderCommented:
did not give an output even if I dont specify any redirection or notification

works fine in normal case
0
 
da99rmdCommented:
sunnycoder,
yes same for me with your version of bash.
/Rob
0
 
sunnycoderCommented:
may be kocil can change bash version requirements ;o)

what bash are you using kocil ?
0
 
alikoankCommented:
if bash is writing this message to stderr you can disable stderr in a script as follows.

exec 2>/dev/null
kill $PID1

I'm nowhere near a unix box now, so I have not tried it.
0
 
sunnycoderCommented:
how about

export $PID1
/bin/bash -c kill $PID1 2>&1 > /dev/null
0
 
sri_prasanthCommented:
hiiii,
After kill $PID1 jst keep clear............:)

i got a doubt if we can make the output of a script be placed in a file because ......if this is possible then we can jst remove this ugly one and get useful things out..........i dun know how 2 do or is this feasible???
0
 
KocilAuthor Commented:
I'm using this for a distro's Install script.
Using ASH, not BASH.
And the echo LA LA LA is actually a dialog.
(Like slackware / debian installation if you can recall).
The "Terminated" message comes out and destroy the dialog,
although I clear the screen quickly after that.

Well, not critical really. but annoying.


I will try this and let you know:

export $PID1
/bin/bash -c kill $PID1 2>&1 > /dev/null



0
 
da99rmdCommented:
Kocil,
alikoank´s sugestion works just add exec 2> /dev/stderr after you kill the process and you enable error messages again.
Other wise it works on the shell me and sonny is using.
/Rob
0
 
KocilAuthor Commented:
Ok, here is the new test
The script
---------------
#!/bin/sh
# use (), setnotify, redirect all output to /dev/null

cat /dev/ttyS0 > /dev/null &
PID=$!
ps
echo PID=$PID
(
unset notify
ps
echo PID=$PID
kill $PID >/dev/null 2>&1
) > /dev/null 2>&1

sleep 1
ps
---------------

The output
--------------
root:# ./aa3
  PID TTY          TIME CMD
 7800 pts/1    00:00:00 bash
 8492 pts/1    00:00:00 aa3
 8493 pts/1    00:00:00 cat
 8494 pts/1    00:00:00 ps
PID=8493
./aa3: line 12:  8493 Terminated              cat /dev/ttyS0 >/dev/null
  PID TTY          TIME CMD
 7800 pts/1    00:00:00 bash
 8492 pts/1    00:00:00 aa3
 8498 pts/1    00:00:00 ps
--------------

So, it is still comes out.

Tested on BASH
GNU bash, version 2.05b.0(1)-release (i486-slackware-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc.

0
 
alikoankCommented:
#!/bin/sh

have you tried changing this to

#!/bin/bash

0
 
KocilAuthor Commented:
This one does not work too.
----------------

#!/bin/sh

cat /dev/ttyS0 > /dev/null &
PID=$!
ps
echo PID=$PID
(
unset notify
ps
echo PID=$PID
exec 2>/dev/null 2>&1
kill $PID >/dev/null 2>&1
) 2>/dev/null
sleep 1
ps

----------------

0
 
KocilAuthor Commented:
Changed to #/bin/bash
Same output.

0
 
KocilAuthor Commented:
Hei ... I found something ...
root:# ./aa3 2>/dev/null
  PID TTY          TIME CMD
 7800 pts/1    00:00:00 bash
 8549 pts/1    00:00:00 bash
 8757 pts/1    00:00:00 aa3
 8758 pts/1    00:00:00 cat
 8759 pts/1    00:00:00 ps
PID=8758
  PID TTY          TIME CMD
 7800 pts/1    00:00:00 bash
 8549 pts/1    00:00:00 bash
 8757 pts/1    00:00:00 aa3
 8764 pts/1    00:00:00 ps

Look, no terminated message !!!
0
 
KocilAuthor Commented:
Ok guys.
This one works.
It is the cat that must be paranthesized.
----------
#!/bin/sh
(                                                                              
cat /dev/ttyS0 > /dev/null &                                                   
ps                                                                              
) 2>/dev/null                                                                  
sleep 1                                                                        
ps                                                                              
killall cat                                                                    
sleep 1                                                                        
ps                                                                              
----------

I answered this by myself, but the idea is from alikoank.
So he get the points.

Thanks for you all that have been with me through this.

0
 
alikoankCommented:
thanks :*)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.