Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

suppress Killed message 1

Status
Not open for further replies.

meetramana

Programmer
Feb 2, 2005
57
US
How do I suppress the Killed message in korn shell?

When I ever I kill a process I see the Killed message in the next command output.

If it is a script I am seeing it right out when I execute it.

Is there a way to suppress the message?

Thanks
 
Killed" means it was sent a SIGKILL (signal number 9), probably using a kill -9 <pid> command. This is generally bad practice unless a process is really misbehaving, because it doesn't give the process a chance to catch the signal and clean up. As a result, you cannot change this behaviour.

Normally processes should be killed (at least on the initial attempt) using the default SIGTERM (signal number 15), which can be trapped and handled to do cleanup, close files, sockets, release memory, IPCs, etc. Just kill <pid> on its own is enough for this.

You can write your own SIGTERM handler using trap 'commands' 15.

Compare the result of these two tests for example:

Code:
$ bash -c 'trap "echo okay i quit" 15 ; kill $$'
$ bash -c 'trap "echo okay i quit" 9 ; kill -9 $$'

Annihilannic.
 
Thanks for your response Annihilannic,

Unfortuanately I don't have bash installed/avaiable.

if I do normal kill (signal 15) in korn like you suggested I get Terminated message in place of killed.

kill <PID> gives : [1] + Terminated <Process Info>
kill -9 <PID> gives : [1] + Killed <Process Info>
 
Sorry, I missed the "korn" in your original post. However the same holds true; if you want to prevent the terminated message, just write your own signal handler using trap.

I forgot to mention, you should include an exit command in your handler otherwise the process will continue to execute!

Code:
#!/usr/bin/ksh

# handle a SIGTERM
trap '
        echo cleaning up!
        exit 0
' 15

# rest of script

Of course, if you want no output at all, just remove the echo statement.

Annihilannic.
 
looks like thats not working either

here is what I am doing

sleep 300 &
ps -ef | grep sleep # ( and catch the pid here )
trap "echo okay i quit; exit 0" 15
kill <pid>

and just hit enter at the command prompt & I see
[1] + Terminated sleep 300 &

AM I doing something wrong?
 
The signal handler has to be implemented in the process you are killing (I was assuming a shell script), not the shell that runs it.

Now that I understand exactly what you want... I think the answer is much simpler! Try set +m (same as set +o monitor).

Annihilannic.
 
you are the man. Wow that was awesome.
Thanks a lot.
Though I actually wanted this in a shell script, I have the habit of making sure it runs in shell first.

so, if I have to do the same in a shell script, everything else without the set +m should be fine, correct?

and everything seemed to have run fine atleast what I was expecting. but why didn't the trap spit out any message, do I have to call that separately?
I am talking about ' trap "echo okay i quit; exit 0" 15 '
when will I see "okay i quit" message? is it saved somewhere until I call it separately?

Thanks a lot again.
 
sorry to bug you..
Actually, I am having an issue using it in the script, it's not working

Here is the scenario,
Script1 calls Script2 which implements process1
and after a certain time I kill process1 from script1

when I execute the script I get
<Path>/Script2[23]: 888842 Killed
where 888842 is the pid that was killed at the time.

I still get this even though I was using set +m and trap.

any ideas?
 
I was trying the trap in Script1 where as the process1 is in script2, I will try the trap in script2.
 
What operating system are you on exactly?

It's strange, when I use an individual sleep to simulate process1, the signal is ignored until the sleep completes, so the message can come up some time later. But if I use a while loop around sleeps it works okay.

So maybe this example will help you?

Code:
$ cat Script1
#!/usr/bin/ksh

print $(date): $0: Starting Script2.
./Script2 &

print $(date): $0: Waiting for a short while...
sleep 3

print $(date): $0: Killing %1.
kill %1

print $(date): $0: Waiting for a short while...
sleep 3

print $(date): $0: Ended normally.

$ cat Script2
#!/usr/bin/ksh
trap '
        print $(date): $0: Cleaning up!
        exit 0
' 15

print $(date): $0: Started.

# simulate process1 working...
i=1
while ((i<15))
do
        sleep 1
        ((i=i+1))
done

print $(date): $0: Ended normally.

$ ./Script1
Fri Jun 25 10:20:02 EST 2010: ./Script1: Starting Script2.
Fri Jun 25 10:20:02 EST 2010: ./Script1: Waiting for a short while...
Fri Jun 25 10:20:02 EST 2010: ./Script2: Started.
Fri Jun 25 10:20:05 EST 2010: ./Script1: Killing %1.
Fri Jun 25 10:20:05 EST 2010: ./Script1: Waiting for a short while...
Fri Jun 25 10:20:05 EST 2010: ./Script2: Cleaning up!
Fri Jun 25 10:20:08 EST 2010: ./Script1: Ended normally.
$

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top