Catch the kill command?

If my process is running with a PID of 1000, is there a way to catch in my code when someone at the terminal types in:  kill 1000 ?

I want to know this so I can free up used resources, and close up log files.
LVL 5
lwinkenbAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RobsonConnect With a Mentor Commented:
Ha!

signal(SIGTERM, sighandler);

SIGKILL is actualluy unblockable and you send it with kill -9

:)

0
 
RobsonCommented:
It's easy to cach a signal:

void sighandler(int number)
{
    printf ("Process is killed!\n");
}

int main()
{
    signal(SIGKILL, sighandler);
    /* rest of your program */
}

But what is exactly that you want? Do you mean that program with pid 1000 is already running and you want to protect it from killing? I don't suppose its possible...
0
 
lwinkenbAuthor Commented:
That didn't work.  I tried the above code, and it just said "Terminated" when I killed it.  It didnt say "Process is killed!".

>> Do you mean that program with pid 1000 is already running and you want to protect it from killing?
The program with pid of 1000 is the small program I am writing here.  This is the one that is catching the signal.
0
 
lwinkenbAuthor Commented:
Thanks =)
0
All Courses

From novice to tech pro — start learning today.