Perl Daemon
Post ReplyPerl DaemonPosted: Wednesday, June 16, 2010 [20:57:17] - 1
Perl daemon is a simple program that runs in a loop indefinitely and runs other programs in event something happens, i.e. some trigger files were written by other programs like Mail etc. Perl Daemon use strict; use POSIX qw(setsid); # flush the buffer $| = 1; # daemonize the program &daemonize; # clean-up directory # run pre-loop programs here or clean the logs, write initial triggers etc. while(1) { # run anything inside this loop # ....... your code goes here ....... # this is main interval for daemon to sleep in seconds sleep(10); } ## while loop end sub daemonize { chdir '/' or die "Can't chdir to /: $!"; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!"; open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; umask 0; } that is it, as simple as that You can start daemon program with cron in /etc/crontab (Tiger and earlier) but you need an intermediate program to start it so you will not be running multiple copies of the same daemon i.e.: open STDOUT, '>>/dev/null'; # your daemon file with full path goes here my $program = "/daemon.cgi"; my $foundit = 0; open (IN, "ps axw |"); while (<IN>) { print "$program was runnig\n"; } close IN; exit(0); and run this script from cron, not the daemon script itself |