Link to home
Start Free TrialLog in
Avatar of Kocil
Kocil

asked on

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.
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi Kocil,

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

Sunnycoder
Avatar of da99rmd
da99rmd

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
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
Avatar of Kocil

ASKER

That does not work Sunny.
Isn't it

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

? (Catch every output)
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
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
pYrania,
I couldent make it work it outputed the **** anyway :(.

/Rob
Hi ,
   Just try it
   kill $PID1 > /dev/zero.
Thats it.
Hari.D
Avatar of Kocil

ASKER

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.


Avatar of Kocil

ASKER

The "unset notify" does not work too.

Man, this is harder than I though.
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)
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
did not give an output even if I dont specify any redirection or notification

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

what bash are you using kocil ?
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.
how about

export $PID1
/bin/bash -c kill $PID1 2>&1 > /dev/null
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???
Avatar of Kocil

ASKER

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



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
ASKER CERTIFIED SOLUTION
Avatar of alikoank
alikoank

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kocil

ASKER

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.

#!/bin/sh

have you tried changing this to

#!/bin/bash

Avatar of Kocil

ASKER

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

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

Avatar of Kocil

ASKER

Changed to #/bin/bash
Same output.

Avatar of Kocil

ASKER

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 !!!
Avatar of Kocil

ASKER

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.

thanks :*)