<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w 

use strict;
use Data::Dumper;

$|=1;

=info
Author                  : Sanjoga Sahu.
Date of Modification    : 03rd, Dec, 2010. (v1.0.0)

Operating System(s)     : Linux

Description             : Runs jobs line crontab.

---------------------------------------------------------------------
Execution Method        :

Options:
    -h : Help
    -d : Debug Printing Mode
    -c : Config file to be used	(No Space between the arguments) [Also Works with Options [config= | conf=]
         Ex: i.   -c/abc/xyz/mycron.conf 
           	 ii.  conf=/abc/xyz/mycron.conf
             iii. config=/abc/xyz/mycron.conf	  

Execution Method :
    1) Running Script Normally (No debug Printing log)... 
        nohup cron_own.pl &amp;

    2) Running Debug Printing Mode (Prints log)... 
        nohup cron_own.pl -d &gt;&gt;! \$HOME/cron_own.log &amp;

	3) Running Script Normally using diffrent config file (No debug Printing log)... 
        nohup cron_own.pl config=/abc/xyz/mycron.conf &amp;

---------------------------------------------------------------------

Config Information      :

Create a config file at : $HOME/config/cron_own.conf

Config Entries are like contab...
	Example:
	Minute   Hour   Day of Month       Month          Day of Week        Command    
	(0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
	0        2          12             *               0,6           /usr/bin/find

Example: (Run in every 00 min of an Hour)

00 * * * * $HOME/MirrorUp_genius_1.0.1.pl I=no &gt;&gt; $HOME/MirrorUp.out 2&gt;&amp;1

---------------------------------------------------------------------

How to check the process and stop script:
#ps -elf | grep cron

0 S XXXXXXXX  27196 26725  0  78   0 -  5759 -      00:04 pts/2    00:00:00 /usr/bin/perl -w cron_own.pl

#kill -9 27196

Will Display:
[1]    Killed                        cron_own.pl &gt;&gt; $HOME/cron_own.log

---------------------------------------------------------------------
=cut

my %INFO=();

my $config="$ENV{'HOME'}/config/cron_own.conf";

my $opt=lc(&amp;trim($ARGV[0]));

my $debug=0;
my $c_flg=0;

	if(defined $opt)
	{
		foreach my $opt_e (@ARGV)
		{
			if($opt_e eq '-h')
			{
			&amp;usage(0);
			exit(0);
			}
			if($opt_e eq '-d')
			{
			$debug=1;
			}
			if($opt_e=~ /(config=|conf=|-c)(\S+)/)
			{
			$config=$2;
			}
			if($opt_e=~ /-c|conf/)
			{
			$c_flg=1;
			}
		}
	}

print "\n" if($debug==1);

	unless(-f $config)
	{
	print "\n[$0] Configuration File NOT Found, Please Create [$config]\n";
	&amp;usage();
	exit(1);
	}
	
	if($c_flg)
	{
	print "[$0] Configuration File Used [$config]\n";
	}
	
my $run=1;

#Infinite While loop which reads the config file in every minute  and run them if the run conditions are met.

	while(1) 
	{
	my $machine=&amp;trim(`uname -n`);
	my $process=$$;
	my $TimeStamp=`date +[%Y-%m-%d' '%H:%M:%S`;
	chomp $TimeStamp;
	$TimeStamp .=" ~ $machine ~ Pid ~ $process]";
	#commnet the below print if no log required...
	print "$TimeStamp $0 Run [$run]...\n" if($debug==1);
	print "$TimeStamp $0 Configuration File Used : $config\n" if($debug==1);
	
	%INFO=();
	&amp;parse_config();
	my $min=&amp;trim(`date +%M`);
	my $hour=&amp;trim(`date +%H`);
	my $date_month=&amp;trim(`date +%d`);
	my $month=&amp;trim(`date +%m`);
	my $day=&amp;trim(`date +%a`);
	$day=&amp;map_day($day);
	
		foreach my $key_job (sort {$a&lt;=&gt;$b} keys %INFO)
		{
		my %RUN=();
		my $c_min =	$INFO{$key_job}{'MIN'};
		my $c_hour=	$INFO{$key_job}{'HOUR'};
		my $c_date_month=$INFO{$key_job}{'DATE_MONTH'};
		my $c_month=$INFO{$key_job}{'MONTH'};
		my $c_day= $INFO{$key_job}{'DAY'};
		my $c_command=$INFO{$key_job}{'COMMAND'};

#		print "$TimeStamp Current Info : MIN:$min, HOUR:$hour, DATE:$date_month, MONTH:$month, DAY:$day\n";
#		print "$TimeStamp Config Final : MIN:$c_min, HOUR:$c_hour, DATE:$c_date_month, MONTH:$c_month, DAY:$c_day, COMMAND:$c_command\n";
		
		#Checking the Run condion of job
		&amp;set_run_flg($c_min,$min,\%RUN);
		&amp;set_run_flg($c_hour,$hour,\%RUN);
		&amp;set_run_flg($c_date_month,$date_month,\%RUN);
		&amp;set_run_flg($c_month,$month,\%RUN);
		&amp;set_run_flg($c_day,$day,\%RUN);
		
			if(defined $RUN{0})
			{
			#skipping the job if run condition is not met
			#commnet the below print if no log required...
			print "$TimeStamp Skipping : $c_command\n" if($debug==1);
			}
			else
			{
			#running the job when run condition is met
			my $child_pid;
				if (!defined($child_pid = fork()))
				{
				die "cannot fork: $!";
				}
				elsif ($child_pid)
				{
				# I'm the parent
				#wait for child job to finish and kills &lt;defunct&gt; process[奴ombies脳
				wait;
				}
				else
				{
				# I'm the child
				#commnet the below print if no log required...
				print "$TimeStamp Fork Process Running  : $c_command\n" if($debug==1);
				#runs the job in fork process ans exits once completed.
				system("nohup $c_command &amp;");
				#print "$TimeStamp Done Running : $c_command, exiting fork process...\n";
				exit(0);
				} 
			}
		#print "\n$TimeStamp NEXT JOB.....\n";
		}
	#commnet the below print if no log required...
	print "$TimeStamp sleep 60...\n\n" if($debug==1);
	sleep 60;
	$run++;
	}


#####################################################################

=head
Checks the config [time] and current [time] and set the flags, whether the job to run or not

=cut

sub set_run_flg
{
my ($config_info,$current_info,$RUN)=@_;

my $flg=0;
my %CONFIG_INP=();
my @CONF=split(/\,/,$config_info);
@CONF= map {trim($_)} @CONF;
@CONF= map {$_=sprintf("%02s",$_);} @CONF;
$current_info=sprintf("%02s",$current_info);

map {$CONFIG_INP{$_}=1} @CONF;
#print Dumper(\%CONFIG_INP);

	if($config_info eq '*')
	{
	$flg=1;
	}
	elsif(defined $CONFIG_INP{$current_info})
	{
	$flg=1;
	}
	else
	{
	$flg=0;
	}

#print "$config_info,$current_info--FLG:::$flg\n";
$$RUN{$flg}=$flg;

return;
}

#####################################################################

=head
Validates the configuration information.


Minute   Hour   Day of Month       Month          Day of Week        Command    
(0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
0        2          12             *               0,6           /usr/bin/find

=cut

sub check_range
{
my ($config_info,$range,$line)=@_;

my ($lower,$higher)=split(/-/,$range,2);
$lower=sprintf("%02s",$lower);
$higher=sprintf("%02s",$higher);

my %flg=();
my @CONF=split(/\,/,$config_info);
@CONF= map {trim($_)} @CONF;
@CONF= map {$_=sprintf("%02s",$_);} @CONF;

#print "$config_info,$range,$higher,$lower----$line---\n";
	if($config_info eq '*')
	{
	$flg{1}=1;
	}
	elsif($config_info=~ /\,/)
	{
	map { if($_ &gt;= $lower &amp;&amp; $_ &lt;=  $higher){ $flg{1}=1; } else { $flg{0}=1; } }@CONF;
	}
	elsif($config_info &gt;= $lower &amp;&amp; $config_info &lt;= $higher)
	{
	$flg{1}=1;
	}
	else
	{
	$flg{0}=1;
	}
	
	if(defined $flg{0})
	{
	print "ERROR &gt;&gt; Incorrect Config Info [$config_info], Ex: [$range], $0 Line Number ",__LINE__, "\n";
	print "LINE  &gt;&gt;$line&lt;&lt;\n";
	exit(1);
	}

return;
}

#####################################################################

#Parse the config File and feeds %INFO hash

sub parse_config
{
	unless (-f $config)
	{
	print "The Cron Own Configuration file NOT Found : $config\n";
	print "Exiting...\n";
	exit(1);
	}

open (RR,$config) || die "Couldn't Open for reading $config : $!\n";
my $count=1;
#print "~~~~~~*****CONFIG PARSE START******~~~~~~\n";
	foreach my $line (&lt;RR&gt;)
	{
	$line=&amp;trim($line);
	next if($line=~/^\#|^$/);
	my $TimeStamp=`date +[%Y-%m-%d' '%H:%M:%S]`;
	chomp $TimeStamp;
	$line=~ s/\s*\,\s*/,/g;
	
	#print "$TimeStamp Config Info : $line\n";
	my ($min,$hr,$date_month,$month,$day_week,$command)=split(/\s+/,$line,6);
	$day_week=&amp;map_day($day_week,$line);
	#print "---$month===\n";
	$month=&amp;map_month($month,$line);
	$min=sprintf("%02s",$min) if ($min ne '*');
	$hr=sprintf("%02s",$hr) if ($hr ne '*');
	$date_month=sprintf("%02s",$date_month) if ($date_month ne '*');
			
	&amp;check_range($min,"0-59",$line);
	&amp;check_range($hr,"0-23",$line);
	&amp;check_range($date_month,"1-31",$line);
	&amp;check_range($month,"1-12",$line);
	&amp;check_range($day_week,"0-6",$line);

	#print "Config Info#:#MIN:$min, HOUR:$hr, DATE:$date_month, MONTH:$month, DAY:$day_week, COMMAND:$command\n";
			
	$INFO{$count}{'MIN'}=$min;
	$INFO{$count}{'HOUR'}=$hr;
	$INFO{$count}{'DATE_MONTH'}=$date_month;
	$INFO{$count}{'MONTH'}=$month;
	$INFO{$count}{'DAY'}=$day_week;
	$INFO{$count}{'COMMAND'}=$command;
	$count++;
	}
#print "~~~~~~~~*****CONFIG PARSE DONE******~~~~~~~~\n\n";
return;
}
#####################################################################
=head
Mapping of day run, returns numaric value or * depending upon the config inputs 
exits if invalid day
=cut
sub map_day
{
my ($day_inp,$line)=@_;

my %MAP=(
	'*' =&gt; '*',
	'0' =&gt; '0',
	'1' =&gt; '1',
	'2' =&gt; '2',
	'3' =&gt; '3',
	'4' =&gt; '4',
	'5' =&gt; '5',
	'6' =&gt; '6',
	'Sun' =&gt; '0',
	'Mon' =&gt; '1',
	'Tue' =&gt; '2',
	'Wed' =&gt; '3',
	'Thu' =&gt; '4',
	'Fri' =&gt; '5',
	'Sat' =&gt; '6',
	'Su' =&gt; '0',
	'Mo' =&gt; '1',
	'Tu' =&gt; '2',
	'We' =&gt; '3',
	'Th' =&gt; '4',
	'Fr' =&gt; '5',
	'Sa' =&gt; '6'
);


my @day=();	
	
	if($day_inp=~ /\,/)
	{
	my @DD=split(/\,/,$day_inp);
		foreach my $d (@DD)
		{
		my $dd=ucfirst($d);
		push(@day,$dd);
		} 
	}
	else
	{
	my $dd=ucfirst($day_inp);
	push(@day,$dd);
	}

my $day;

	foreach my $dd (@day)
	{
		if(defined $MAP{$dd})
		{
		$day .="$MAP{$dd},";
		}
		else
		{
		print "ERROR &gt;&gt; Invalid Day [$day_inp] Provided in config File : $config, $0 Line Number ",__LINE__, "\n";
		print "LINE  &gt;&gt;$line&lt;&lt;\n";
		exit(1);
		}
	}

chop $day;

return $day;
}
#####################################################################

=head
mapping of month retuns numeric value or * depending on the config info
exits if invalid month
=cut

sub map_month
{
my ($month_inp,$line)=@_;

#print "--$month_inp--==$line--\n";

my %MAP=(
	'*' =&gt; '*',
	'1' =&gt; '01',
	'2' =&gt; '02',
	'3' =&gt; '03',
	'4' =&gt; '04',
	'5' =&gt; '05',
	'6' =&gt; '06',
	'7' =&gt; '07',
	'8' =&gt; '08',
	'9' =&gt; '09',
	'01' =&gt; '01',
	'02' =&gt; '02',
	'03' =&gt; '03',
	'04' =&gt; '04',
	'05' =&gt; '05',
	'06' =&gt; '06',
	'07' =&gt; '07',
	'08' =&gt; '08',
	'09' =&gt; '09',
	'10' =&gt; '10',
	'11' =&gt; '11',
	'12' =&gt; '12',
	'Jan' =&gt; '1',
	'Feb' =&gt; '2',
	'Mar' =&gt; '3',
	'Apr' =&gt; '4',
	'May' =&gt; '5',
	'Jun' =&gt; '6',
	'Jul' =&gt; '7',
	'Aug' =&gt; '8',
	'Sep' =&gt; '9',
	'Oct' =&gt; '10',
	'Nov' =&gt; '11',
	'Dec' =&gt; '12',
	'Ja' =&gt; '1',
	'Fe' =&gt; '2',
	'Ma' =&gt; '3',
	'Ap' =&gt; '4',
	'Ma' =&gt; '5',
	'Ju' =&gt; '6',
	'Ju' =&gt; '7',
	'Au' =&gt; '8',
	'Se' =&gt; '9',
	'Oc' =&gt; '10',
	'No' =&gt; '11',
	'De' =&gt; '12'

);

my $month="";
	
my @month=();	
	
	if($month_inp=~ /\,/)
	{
	my @mon=split(/\,/,$month_inp);
		foreach my $m (@mon)
		{
		my $mon=ucfirst($m);
		push(@month,$mon);
		} 
	}
	else
	{
	my $mon=ucfirst($month_inp);
	push(@month,$mon);
	}

	foreach my $mon (@month)
	{
		if(defined $MAP{$mon})
		{
		$month .="$MAP{$mon},";
		}
		else
		{
		print "ERROR &gt;&gt; Invalid Month [$month_inp] Provided in config File : $config, $0 Line Number ",__LINE__, "\n";
		print "LINE  &gt;&gt;$line&lt;&lt;\n";
		exit(1);
		}
	}
chop $month;
return $month;
}

#####################################################################

=head 

This Subroutine trims pre-post white Spaces/new line from the string

Arguments:

1. $var (input String)

=cut

sub trim
{
my ($var)=@_;
return unless (defined $var);
$var=~s /^\s+|\s+$//g;
return $var;
}

#####################################################################

sub usage
{

my $INFO=&lt;&lt;EOD;	

Usgage : $0
  
  Options:
    -h : Help
    -d : Debug Printing Mode
    -c : Config file to be used	(No Space between the arguments) [Also Works with Options [config= | conf=]
         Ex: i.   -c/abc/xyz/mycron.conf 
           	 ii.  conf=/abc/xyz/mycron.conf
             iii. config=/abc/xyz/mycron.conf	  

Execution Method :
    1) Running Script Normally (No debug Printing log)... 
        nohup cron_own.pl &amp;

    2) Running Debug Printing Mode (Prints log)... 
        nohup cron_own.pl -d &gt;&gt;! \$HOME/cron_own.log &amp;

	3) Running Script Normally using diffrent config file (No debug Printing log)... 
        nohup cron_own.pl config=/abc/xyz/mycron.conf &amp;

	
***Configuration File Name : $config***

Note : If Configuration File is Not Available, then Create One with Above name.  

##################################################################################
####                                                                          #### 
## Configuration Parameter Description :                                        ##
##                                                                              ##
## Minute   Hour   Day of Month       Month          Day of Week        Command ##    
## (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)             ##                 
##                                                                              ##
## **************************************************************************** ##
## Example : (Run in every 00 min of an Hour)                                   ## 
##                                                                              ##
## 00 * * * * /abspath/script_name &gt;&gt; /abspathlog/logname 2&gt;&amp;1                  ##
####                                                                          ####
##################################################################################
EOD

print "$INFO\n";
return;
}
</pre></body></html>