in reply to Daemons???
Here is the basis for a general daemon. Add code at the end to do what you want. This follows the Stevens rules.
If you open communication channels before the fork, the child will inherit their handles.#!/usr/bin/perl -w use strict; use POSIX qw( setsid ); my $debug = 1; my $logfile = q(.testlog); # Season to taste my @fh_unused = (\*STDIN, \*STDOUT); open \*STDERR, ">> $ENV{'HOME'}/$logfile"; select((select(\*STDERR), $| = 1)[0]); { # Daemon Rule 1) Fork and exit the parent. my $ppid = $$; my $pid = fork and exit 0; ! defined $pid and die "No Fork: ", $!; while (kill 0, $ppid) { select undef, undef, undef, .001; }; } # Daemon Rule 2) become session leader, pg leader, no term my $session_id = POSIX::setsid(); # Daemon Rule 3) cd to / chdir '/' or die "Could not cd to rootfs", $!; # Daemon Rule 4) set file creation mask to 0 my $oldmask = umask 00; # Daemon Rule 5) Close unneeded file handles close $_ or die $! for @fh_unused; # Your code here exit 0;
After Compline,
Zaxo
|
---|