Solved

Kill background process, but no display

Posted on 2004-08-02
29
1,111 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:Kocil
  • 9
  • 7
  • 5
  • +4
29 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11691607
Hi Kocil,

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

Sunnycoder
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 11691649
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
 
LVL 8

Expert Comment

by:da99rmd
ID: 11691664
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
 
LVL 5

Author Comment

by:Kocil
ID: 11691936
That does not work Sunny.
0
 
LVL 3

Expert Comment

by:pYrania
ID: 11692544
Isn't it

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

? (Catch every output)
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 11692591
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
 
LVL 3

Expert Comment

by:pYrania
ID: 11692670
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
 
LVL 8

Expert Comment

by:da99rmd
ID: 11692820
pYrania,
I couldent make it work it outputed the **** anyway :(.

/Rob
0
 

Expert Comment

by:hvd24
ID: 11705835
Hi ,
   Just try it
   kill $PID1 > /dev/zero.
Thats it.
Hari.D
0
 
LVL 5

Author Comment

by:Kocil
ID: 11711946
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
 
LVL 5

Author Comment

by:Kocil
ID: 11711955
The "unset notify" does not work too.

Man, this is harder than I though.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11712334
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
 
LVL 8

Expert Comment

by:da99rmd
ID: 11712989
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11713216
did not give an output even if I dont specify any redirection or notification

works fine in normal case
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 8

Expert Comment

by:da99rmd
ID: 11713241
sunnycoder,
yes same for me with your version of bash.
/Rob
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11713311
may be kocil can change bash version requirements ;o)

what bash are you using kocil ?
0
 
LVL 4

Expert Comment

by:alikoank
ID: 11713916
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11714236
how about

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

Expert Comment

by:sri_prasanth
ID: 11717356
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
 
LVL 5

Author Comment

by:Kocil
ID: 11722430
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
 
LVL 8

Expert Comment

by:da99rmd
ID: 11723555
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
 
LVL 4

Accepted Solution

by:
alikoank earned 125 total points
ID: 11723621
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
 
LVL 5

Author Comment

by:Kocil
ID: 11733345
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
 
LVL 4

Expert Comment

by:alikoank
ID: 11733350
#!/bin/sh

have you tried changing this to

#!/bin/bash

0
 
LVL 5

Author Comment

by:Kocil
ID: 11733361
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
 
LVL 5

Author Comment

by:Kocil
ID: 11733365
Changed to #/bin/bash
Same output.

0
 
LVL 5

Author Comment

by:Kocil
ID: 11733396
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
 
LVL 5

Author Comment

by:Kocil
ID: 11733437
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
 
LVL 4

Expert Comment

by:alikoank
ID: 11734087
thanks :*)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now