Setting up a Daemon
- The FAM daemon cannot actually execute a command.
- Frequently want to set up a daemon process to do the monitoring:
#!/usr/bin/perl -w
use strict;
use SGI::FAM;
my $monitor_file = '/some/interesting/file';
my $pid_file = '/var/run/some.pid';
my $pid = fork();
if ($pid) {
# new pid number to log
open(PID_LOG, "> $pid_file") || die "open($pid_file): $!";
print PID_LOG "$pid\n";
close(PID_LOG);
print "Started with pid = $pid \n";
exit(1);
}
else {
# In the child process
$pid = $$;
# open log file and pipe stdout and stderr to it
open(LOG, "| /usr/bin/logger -t $proc_name") || exit ;
select(LOG);
$| = 1;
select(STDOUT);
print LOG "\n", '=' x 78, "\nStarting $proc_name at: " .
localtime(), " Process ID: $pid\n\n";
close(STDOUT);
open(STDOUT, ">&LOG") || exit;
close(STDERR);
open(STDERR, ">&LOG") || exit;
my $fam = new SGI::FAM;
$fam->monitor($monitor_file);
while (1) {
my $event = $fam->next_event; # blocks here
print "Heh! $monitor_file has changed\n";
}
}