Google
 
 
Home arrow Perl Articles arrow Catch Kill Signals In Perl

Main Menu
 Home
 Linux Articles
 FreeBSD Articles
 Apache Articles
 Perl Articles
 Other Articles
 Program Downloads
 Free Books
 News
 The Web Links
 Contact Us

Most Read
Automating SFTP using expect
FreeBSD PPTP VPN
Shorewall Router on Linux
SnortShorwall - Using Snort And Shorewall Together
Shorewall Stand Alone Firewall

Polls
Favorite Linux/BSD
Fedora
Mandrake
Debian
Slackware
Gentoo
Suse
FreeBSD
Other
  

Syndicate
Latest news direct to your desktop
RSS

Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one

Members Online
 Linux-BSD-Central Has a Total of 701 Members   Members (701) # Online
 We have 6 Guests Online. Guests 6
 We have 0 Users Online. Users 0

Online Users
No Users Online

Statistics
OS: Linux w
PHP: 5.2.9
MySQL: 5.0.91-community
Time: 14:55
Members: 701
Hits: 1324418
News: 277
WebLinks: 15



-->

Catch Kill Signals In Perl   PDF  Print  E-mail 
Contributed by Chad Brandt  
Saturday, 03 July 2004
When you create a perl program that will run in the background as a deamon, it is a good idea to catch kill signals and do cleanup processing in your program before it exits. Perl has a sigtrap function that make this very easy to do.

The following example shows a perl program running as a deamon that prints messages when a signal is sent to it. This demonstrates how you can catch signals and do the neccassary processing

#!/usr/bin/perl
############################################################
# This is a sample program that shows how you can catch
# signals to your program when running as a deamon
#
############################################################

use
sigtrap 'handler' => \&reload, 'HUP';
use
sigtrap 'handler' => \&cleanAndExit, 'INT', 'ABRT', 'QUIT', 'TERM';

# create the deamon / kill parent
if
(fork()){
    exit(0);
}

print
"New PID - $$\n";

for(;;){
    # do your deamon work here
    sleep(1);

}

# refresh the config info and continue processing
# this method will be called if the following signal is given to your pid
# kill -HUP
sub
reload() {

    print "Caught a HUP signal, reload config files and   continue processing\n";
}

# close all file handles and do any other clean up
# this method will be called if the followng signals are given to your pid
# kill -INT
# kill -ABRT
# kill -QUIT
# kill -TERM
sub
cleanAndExit(){

    print "Caught a kill signal, cleaning up and  exiting\n";
    exit(1);
}

 

Comments

Write Comment
Name:Guest
Title:
BBCode:Web AddressEmail AddressBold TextItalic TextUnderlined TextQuoteCodeOpen ListList ItemClose List
Comment:



Powered by AkoComment 1.0 beta 2!




 
Google Ads



 

Check out TwistByte - The best mobile apps available For awesome Android and IPhone applications!!