# # # http://www.unix.org.ua/orelly/perl/cookbook/ch17_16.htm # # # You want your program to run as a daemon. # ========================================== # If you are paranoid and running as root, chroot to a safe directory: chroot("/var/daemon") or die "Couldn't chroot to /var/daemon: $!"; # Fork once, and let the parent exit. $pid = fork; exit if $pid; die "Couldn't fork: $!" unless defined($pid); # Dissociate from the controlling terminal that started us # and stop being part of whatever process group we had been a member of. use POSIX; POSIX::setsid() or die "Can't start a new session: $!"; # Trap fatal signals, setting a flag to indicate we need to gracefully exit. $time_to_die = 0; sub signal_handler { $time_to_die = 1; } $SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler; # trap or ignore $SIG{PIPE} # Wrap your actual server code in a loop: until ($time_to_die) { # ... } # # # End of file