Discussion:
Bash: kill without output
(too old to reply)
d***@gmail.com
2007-08-23 15:09:12 UTC
Permalink
Hi all,

Probably a stupid question, but I have been searching for an answer
for quite some time.
I have a bash script, in which I create a process that runs in the
background, and that needs to be killed at a certain point in this
script. However, I do not want to get a message in my terminal about
the killing of this process.
Let me show you what I do right now:

# Create the process running in the background (i.e. make sure
everything that arrives on /dev/ttyS1 is printed on stdout
cat /dev/ttyS1 &
PID=$!
# Do some other stuff
...
# Kill the cat-process
kill $PID

If I run this script, I get the following output:
./name_of_the_script: line line_of_kill_instructiong: <processID>
Killed cat /dev/ttyS1
As my script has the intention of showing some information in a nice
way on stdout, this output disturbs that intention.

Thanks in advance for all help,

Dirk
Vilmos Soti
2007-08-23 15:47:05 UTC
Permalink
Post by d***@gmail.com
Probably a stupid question, but I have been searching for an answer
for quite some time.
I had exactly the same problem a few weeks ago.
Post by d***@gmail.com
I have a bash script, in which I create a process that runs in the
background, and that needs to be killed at a certain point in this
script. However, I do not want to get a message in my terminal about
the killing of this process.
...
Post by d***@gmail.com
cat /dev/ttyS1 &
Try running your program in a subshell and redirect stderr to devnull.
Maybe you want to put the redirection outside the parentheses.
My problem was a bit different (uninterested in any output), so I
cannot give you a definite answer.

(cat /dev/ttyS1 2> /dev/null &)

Vilmos
Vilmos Soti
2007-08-23 17:50:56 UTC
Permalink
Post by Vilmos Soti
Post by d***@gmail.com
I have a bash script, in which I create a process that runs in the
background, and that needs to be killed at a certain point in this
script. However, I do not want to get a message in my terminal about
the killing of this process.
...
Post by d***@gmail.com
cat /dev/ttyS1 &
Try running your program in a subshell and redirect stderr to devnull.
Maybe you want to put the redirection outside the parentheses.
My problem was a bit different (uninterested in any output), so I
cannot give you a definite answer.
(cat /dev/ttyS1 2> /dev/null &)
Actually, you don't need any redirection. Just put the whole
thing into a subshell, and put it into the background.
Important, the ampersand needs to be *INSIDE* the subshell.

Sleeping with and w/o subshell.

$ sleep 20 &
[1] 5729
$ killall sleep
[1]+ Terminated sleep 20
$
$ (sleep 20 &)
$ ps
PID TTY TIME CMD
24057 tty5 00:00:00 bash
5744 tty5 00:00:00 sleep
5748 tty5 00:00:00 ps
$ killall sleep
$
$
$
$ ps
PID TTY TIME CMD
24057 tty5 00:00:00 bash
5765 tty5 00:00:00 ps
$

The ampersand inside/outside the shell.

$ (sleep 20) &
[2] 6207
$ killall sleep
$
[2]- Terminated ( sleep 20 )
$ (sleep 20 &)
$ killall sleep
$
$

About redirecting stderr to /dev/null. It is not needed, and
it won't help, since the message is printed *NOT* by the
program but by the shell.

Vilmos

Amy Lee
2007-08-23 15:54:24 UTC
Permalink
Post by d***@gmail.com
Hi all,
Probably a stupid question, but I have been searching for an answer for
quite some time.
I have a bash script, in which I create a process that runs in the
background, and that needs to be killed at a certain point in this script.
However, I do not want to get a message in my terminal about the killing
of this process.
# Create the process running in the background (i.e. make sure everything
that arrives on /dev/ttyS1 is printed on stdout cat /dev/ttyS1 &
PID=$!
# Do some other stuff
...
# Kill the cat-process
kill $PID
line line_of_kill_instructiong: <processID> Killed cat /dev/ttyS1
As my script has the intention of showing some information in a nice way
on stdout, this output disturbs that intention.
Thanks in advance for all help,
Dirk
kill $PID > /dev/null

Regards,

Amy Lee
Continue reading on narkive:
Loading...