Skip to Main Content
Perl Cookbook
book

Perl Cookbook

by Tom Christiansen, Nathan Torkington
August 1998
Intermediate to advanced content levelIntermediate to advanced
800 pages
39h 20m
English
O'Reilly Media, Inc.
Content preview from Perl Cookbook

Restarting a Server on Demand

Problem

You want your server to shutdown and restart when it receives a HUP signal, just like inetd or httpd.

Solution

Catch the SIGHUP signal, and re-execute your program:

$SELF = "/usr/local/libexec/myd";   # which program I am
@ARGS = qw(-l /var/log/myd -d);     # program arguments

$SIG{HUP} = \&phoenix;

sub phoenix {
    # close all your connections, kill your children, and
    # generally prepare to be reincarnated with dignity.
    exec($SELF, @ARGS)              or die "Couldn't restart: $!\n";
}

Discussion

It sure looks simple (“when I get a HUP signal, restart”) but it’s tricky. You must know your own program name, and that isn’t easy to find out. You could use $0 or the FindBin module. For normal programs, this is fine, but critical system utilities must be more cautious, as there’s no guarantee that $0 is valid. You can hardcode the filename and arguments into your program, as we do here. That’s not necessarily the most convenient solution, however, so you might want to read the program and arguments from an external file (using the filesystem’s protections to ensure it hasn’t been tampered with).

Be sure and install your signal handler after you define $SELF and @ARGS, otherwise there’s a race condition when a SIGHUP could run restart but you don’t know the program to run. This would cause your program to die.

Some servers don’t want restart on receiving a SIGHUP—they just want to reread their configuration file.

$CONFIG_FILE = "/usr/local/etc/myprog/server_conf.pl"; $SIG{HUP} ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl in a Nutshell

Perl in a Nutshell

Nathan Patwardhan, Ellen Siever, Stephen Spainhour
Perl Best Practices

Perl Best Practices

Damian Conway
Mastering Perl

Mastering Perl

brian d foy
Perl Cookbook, 2nd Edition

Perl Cookbook, 2nd Edition

Tom Christiansen, Nathan Torkington

Publisher Resources

ISBN: 1565922433Supplemental ContentCatalog PageErrata