Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

To get first non-active date from cron entry

by Anonymous Monk
on Mar 01, 2011 at 05:33 UTC ( #890681=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I'm new to perl. I am using cron entry in my program, in that I need to find the first non active date from given cron entry. I don't have any idea instead of parsing the cron entry manually. Is there any module available for my requirement? Thanks in Advance.
  • Comment on To get first non-active date from cron entry

Replies are listed 'Best First'.
Re: To get first non-active date from cron entry
by mellon85 (Monk) on Mar 01, 2011 at 06:44 UTC
      there must be good CPAN module. Below is not good...
      I wonder why my code becomes so ugly...
      use strict; use warnings; use Date::Manip::Date; my $date=new Date::Manip::Date; open my $fh, '<', "/etc/crontab" or die $!; foreach my $line (<$fh>){ chomp $line; #skip SHELL=,PATH=,comment,empty line next if ( $line=~ /^SHELL/ || $line =~ /^PATH/ || $line =~ /^#/ || + $line=~ /^$/); #skip @reboot,@hourly, etc next if ( $line =~ /^@/ ); my ($min, $hour, $day, $month)=(split(/\s+/, $line))[0 .. 3]; foreach my $target (\$min, \$hour, \$day, \$month){ $$target=expand2commasep($$target); } my $error=""; foreach my $e_month (split(/,/, $month)){ foreach my $e_day (split(/,/, $day)){ foreach my $e_hour (split(/,/, $hour)){ foreach my $e_min(split(/,/, $min)){ $e_min=sprintf("%02d",$e_min); $e_hour=sprintf("%02d",$e_hour); $e_day=sprintf("%02d",$e_day); $e_month=sprintf("%02d",$e_month); if( $date->parse_format( "%Y/%m/%d %H:%M:%S", ,"2011/$e_month/$e_day $e_hour:$e_min:01")){ $error.="#############################\n"; $error.="LINE=$line\n"; $error.="target date/time=2011/$e_month/$e_day + $e_hour:$e_min:01\n $error.=$date->err . "\n"; } } } } } if( $error ne ''){ print $error; } } close $fh; sub expand2commasep{ my ($val) = @_; # 1-9 format to comma separated list while ( $val =~ /(\d+)-(\d+)/ ){ my $buff=''; foreach my $item ( $1 .. $2){ $buff .= $buff eq '' ? $item : "," . $item; } $val =~ s/\d+-\d+/$buff/; } #*/d format to 1 if ($val =~ m^\*/(.*)^ ){ if ($1 =~ /\d+/){ $val=~ s^(\*/\d+)^1^; } else { print "error: not numeric $val\n"; } } #* to 1 if ($val =~ /\*/ ){ $val=~ s/\*/1/; } return($val); }
        Because you're hand parsing a crontab, instead of using some cpan module to do it for you?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://890681]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2022-12-01 17:54 GMT
Find Nodes?
    Voting Booth?