BEGIN { # allow script to be reinitialized with a HUP signal $SIG{HUP} = sub { $HUP = 1; warn "Caught HUP\n" }; } # main loop while ( 1 ) { $conn = accept_new_conn(); # check if we have been HUPPED if ( $HUP ) { REHUP: # reset the flag $HUP = 0; warn "***** Widget reinitializing\n"; do_disconnect(); do_init(); # now it is possible, but unlikely that we get another SIG HUP while # we are performing our reinit routines so we need to redo this # stuff if that occurs, otherwise we will be partially reinitialised goto REHUP if $HUP; } # more code here } #### apache ALL=NOPASSWD:/home/www/utility/sendHUP.pl #### #!/usr/bin/perl -w # sendHUP.pl # this script need to be run as root, to do this we add an entry to # /etc/sudoers so that apache can run it (you edit using visudo) # visudo -f /etc/sudoers # add this line # apache ALL=NOPASSWD:/devel/www/utility/sendHUP.pl # call as system('sudo', '/devel/www/utility/sendHUP.pl'); $|++; #use strict; use Fcntl; use POSIX qw(setsid); # we really really don't want to half HUP the widgets # this can potentially occur so we ignore INT and TERM SIGs BEGIN { $SIG{INT} = $SIG{TERM} = sub { warn $SIG{INT} ? "*****sendHUP caught INT" : "*****sendHUP caught TERM" } } my $PROGRAM = 'widget.pl'; my @ps = `ps -C $PROGRAM`; @ps = map { m/(\d+)/; $1 } grep { /\Q$PROGRAM\E/ } @ps; # now demonize it defined(my $pid = fork) or die "Can't fork: $!"; exit 0 if $pid; chdir '/' or die "Can't chdir to /: $!"; umask 0; setsid() or die "Can't start a new session: $!"; for ( @ps ) { (kill HUP, $_) or warn "***** Failed to HUP $_\n"; } my $time = gmtime(); warn "[$time] Sent SIGHUP to $PROGRAM @ps\n"; exit 0;