Thread: Is it possible to let a program capture SIGKILL ?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Is it possible to let a program capture SIGKILL ?

    Is it possible to let a program itself capture a Kill Signal, write a log, then exit?
    Say, a program is called myprog. you use
    Code:
    killall myprog
    to kill it in linux.

    Instead of exiting immediately, myprog writes a log about someone is killing it, and then it exits? Is there a such way?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Alas, Windows is indeed a rare breed of operating systems. Not many allow a programmer to do stuff after the OS categorically told it to die. There is no easy means of doing what you want as far as I know. But I bet you could probably change your kernel a little to allow this practice.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    When you "killall myprog", myprog receives a signal, right? So can we capture the signal in myprog?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Signal handlers cannot be set for SIGKILL or SIGSTOP.
    http://man.he.net/?topic=signal&section=all

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You cannot catch SIGKILL. If a process receives this signal, it immediately terminates.

    You can catch pretty much any other signal, though. And 'killall' uses SIGTERM, not SIGKILL, by default. The standard practice is to catch SIGTERM.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As far as I know, the only option for what you want to do is to have another program trace the SIGKILLs that are sent. From the perspective of your program, by the time its received the signal it will soon die and has no way of doing anything once the signal is received.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Call a program & still capture cmds
    By Zuconta in forum C Programming
    Replies: 3
    Last Post: 07-23-2003, 03:54 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM