trap kill -9


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trap kill -9
# 1  
Old 02-01-2012
trap kill -9

Is there a way to trap kill -9 pid? or is there any other way to detect kill -9 so that some files can be deleted before the process ends?
# 2  
Old 02-01-2012
The trap command can catch signals being sent but I don't think you can trap a SIGKILL signal. Can you trap another signal instead and use that for graceful shutdown?
Simple ksh93 script to illustrate, run it:
Code:
$ cat efs
#!/usr/dt/bin/dtksh

trap 'print TERM received;exit 1' 15

while :
do
  sleep 5
done

exit 0
$

Then open another terminal window and send a 15 to the process above:
Code:
kill -15 <PID>

You will see in the first window:
Code:
$ efs
TERM received
$


Last edited by gary_w; 02-01-2012 at 04:12 PM..
This User Gave Thanks to gary_w For This Post:
# 3  
Old 02-01-2012
No. You cannot trap "kill -9".

The "kill -9" is an absolute last resort and IMHO should never appear in a script unless preceded by reasonable attempts to remove the bad process according to the Signals specified for the software. This is paramount for database software where it is important to learn the correct signals and to use database tools rather than unix "kill".

The best fix for "kill -9" is to never issue the command except where you can prove that that killing the process will not cause data corruption.


@gary_w
(Just read your amended post). There is no problem trapping "kill -15" (HANGUP) and I rely on this for many a cleanup. Some O/S can take 2 hours to realise that a connection has disappeared and then record a HANGUP. The issue is with "kill -9" which should never be issued by anybody other than an expert. I have shut down databases rather than issue "kill -9" to database clients in the full knowledge that the database engine can deal with the situation much better than someone working at a unix command prompt.
However you do occasionally come across looping client processes with no database connection and no parent process (except "init" PID 1) which need a "kill -9" to get rid of them.

Btw. I've never had a problem with "trap '<cleanup>' 1 2 3 15"

Last edited by methyl; 02-01-2012 at 04:32 PM..
These 3 Users Gave Thanks to methyl For This Post:
# 4  
Old 02-01-2012
I totally agree with avoiding kill -9. That is always a last resort in my opinion. I was just suggesting to the original poster that maybe he could construct his script to use a different signal to allow him to do some cleanup upon receipt of that signal, and to show an example in case he was not sure how to set that up.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

2. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

Good afternoon I need to KILL a process in a single command sentence, for example: kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'` That sentence Kills the process ID corresponding to the program CAL255.4ge. However it is possible that the same program... (6 Replies)
Discussion started by: enriquegm82
6 Replies

3. Shell Programming and Scripting

How to Trap kill -9 signal

I just want to trap kill -9 signal issued by any of user from any terminal and just capture that user terminal who had raised this kill -9 command (1 Reply)
Discussion started by: puneet.goel
1 Replies

4. Linux

Kill a process without using kill command

I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Discussion started by: sudhamacs
6 Replies

5. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

6. Shell Programming and Scripting

Kill a process without using kill command

Sorry, posted the question in other forum. (0 Replies)
Discussion started by: sudhamacs
0 Replies

7. UNIX for Advanced & Expert Users

Kill all child processes on trap

hi OS: Sun Solaris I have a scenario that when someone presses ctrl-c while executing a shell script, it should not just exit. it should kill all the child processes started by the running shell script only. I am executing many other scripts parallely which in turn fork off more... (2 Replies)
Discussion started by: rakeshou
2 Replies

8. Programming

kill(0,-9) don't kill the process

Hi all i have simple c program , when i wish to kill the app im using kill(0,-9) , but it seams this command don't do any thing and the program. just ignore it . what im doing wrong here ? im using HP-UX ia64 Thanks (9 Replies)
Discussion started by: umen
9 Replies

9. UNIX for Advanced & Expert Users

When kill doesnt work, how to kill a process ?

Hi All, I am unable to kill a process using kill command. I am using HP-UX system. I have tried with kill -9 and i have root privilages. How can i terminate this daemon ? ? ? Regards, Vijay Hegde (3 Replies)
Discussion started by: VijayHegde
3 Replies

10. Shell Programming and Scripting

Building a better mouse trap, or How many lines of code does it take to trap a mouse?

Hello all, I'm hoping to get a little insight from some of the wily veterans amongst you. I've written a script to check for new outgoing files to our vendors located on our ssl server. It seems to be working ok, but the final question here, will be one of logic, and/or a better way to... (4 Replies)
Discussion started by: mph
4 Replies
Login or Register to Ask a Question