Re: Redirecting output from "kill" command?

From: Greg Porr (gporr@gscdump)
Date: 04/02/03


From: gporr@gscdump (Greg Porr)
Date: 2 Apr 2003 10:44:39 -0500

In article <3E8A274D.34D5461C@alcyone.com> Erik Max Francis <max@alcyone.com> writes:
>Bub wrote:
>>
>> Thanks Erik, but I've tried that (the basic " 2> /dev/null") and it
>> doesn't work either...
>
>I suspect you're doing something wrong. Are you sure you're using a
>Bourne-like shell?
>
>max@oxygen:~% kill 123123123
>kill: kill 123123123 failed: no such process
>max@oxygen:~% kill 123123123 2> /dev/null
>max@oxygen:~%

If it's the "Terminated" message which you're trying to redirect,
that's written to stderr by the parent shell whose child has been killed,
not by the kill command itself. You'd have to redirect the parent shell's
stdout, but then you'd lose stuff like prompts, as well as the shell's
displaying of any background process ids.

Take a look at the following:

gporr@gscdump: truss -f -wall -o truss.out /bin/sh
gporr@gscdump: sleep 100 &
25797
gporr@gscdump: /bin/kill 25797
25797 Terminated
gporr@gscdump: exit

We can see all the processes involved:

gporr@gscdump: grep exec truss.out
25165: execve("/bin/sh", 0x08047C30, 0x08047C38) argc = 1
25797: execve("/usr/bin/sleep", 0x0806A164, 0x0806A344) argc = 2
25799: execve("/bin/kill", 0x08069F9C, 0x08069FA8) argc = 3
25801: execve("/bin/sh", 0x08069FC8, 0x08069FD8) argc = 3

Pid 25165 is the parent shell, pid 25797 is the sleep process which we
end up killing, pid 25799 is /bin/kill (a shellscript), and pid 25801
is another /bin/sh process kicked off by /bin/kill. It's pid 25801 which
actually executes the kill() system call.

Here are the relevant lines from the truss output which show pid 25165
(the parent shell) writing the "Terminated" message
to file descriptor 2 (stderr) :

25165: write(2, 0x080673EC, 17) = 17
25165: 32 35 37 39 37 20 54 65 72 6D 69 6E 61 74 65 64 !25797 Terminated !

Note that if you used "kill" instead of "/bin/kill", that's a shell builtin,
so /bin/kill and the other /bin/sh process wouldn't be needed.
In that case, the parent shell (pid 25165) would end up making the kill()
system call, but 25165 still waits for 25797 and then displays
the "Terminated" message.

Hope that helps.

- Greg Porr, NCR Corporation



Relevant Pages

  • Re: External Unix programme - time limit
    ... checking if the process is still active after the sleep. ... >> kill the right programme...and when using the syntax you ... > You don't need to know the PID, %1 is the first job running in the ... > shell, which should be your ./senkin.e program. ...
    (comp.soft-sys.matlab)
  • Re: Fork, exec - setsid?
    ... So I came up with the need to fork some processes away ... > of those or the spawned shell. ... The shell will have the pid that forkreturned to the parent. ... > How do I kill the forked away programms without knowing their PID? ...
    (comp.lang.perl.misc)
  • Re: Wait command
    ... > If it wasn't your wait command that reaped processA what was it? ... If the process wasn't reaped until you manually ran a wait in the shell ... > ps and kill with an interval between and know that the PID of interest ...
    (comp.unix.solaris)
  • Re: Find exact creation time of a process; not kill a wrong process
    ... > How can I kill a process without worrying killing a wrong process? ... > The $pid may be a recycled new process. ... Use Job Control. ... And you're using a shell script for process monitoring??? ...
    (comp.unix.programmer)
  • Re: Find exact creation time of a process; not kill a wrong process
    ... > How can I kill a process without worrying killing a wrong process? ... > The $pid may be a recycled new process. ... Use Job Control. ... And you're using a shell script for process monitoring??? ...
    (comp.unix.shell)