Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Daemon

Status
Not open for further replies.
May 3, 2002
633
US
I am having a problem with cron jobs running for this particular job, therefore I want to create a daemon in perl to run the job, and it works, until I try passing day_of_week, hour, and min in the "if" statement. It works with just dow and hour and prints the message and sleeps. I always work with ksh and hardly ever perl, but unfortunately cannot create a daemon in ksh that I know of so am using perl. How do I get this to work properly. Below is the script. Thanks.

#!/usr/bin/perl

# load required modules
use strict;
use POSIX qw(setsid);
use POSIX qw(strftime);

# set constants
my $DoW = 'Wed';
my $Hour = '13';
my $Min = '11';
my $GetDoW = `date +%a`;
my $GetHour = `date +%H`;
my $GetMin = `date +%M`;

# flush the buffer
$| = 1;

# daemonize the program
&daemonize;

# infinite loop
while(1) {
if ( $GetDoW == $DoW && $GetHour == $Hour && $GetMin == $Min ) {
printf("test message\n");
}
sleep(60)
}

sub daemonize {
chdir '/' or die "Can't chdir to /: $!";
umask 0;
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: $!";
}
 
The problem here is that the value of $GetMin is set and doesn't change. That way as it goes infinitely, it is always set to 11 and doesn't change with each passing minute. I'd just go ahead and put the `` statement in instead of assigning it as a variable. this will fix it. --Derek

"Fear not the storm for this is where we grow strong."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top