1

I want to set up a cron job which will execute a command every hour. However, I want that this command should be started at 10 A.M and should run every hour till 4 P.M. This job is to run daily between these times. The command is nothing but a call to a Perl script. Following crontab entry runs fine and invokes the script every hour

* */1 * * * cd path_to_file; perl file.pl > path_to_logs/log.txt

Is there a way to limit the timings of this cron job so that it runs only between 10 A.M and 4 P.M ?

1
  • Add another cron job at 4PM that kills it?
    – zmccord
    Apr 23, 2012 at 5:33

3 Answers 3

6

man 5 crontab is your friend. (Your example does not do what you claim it does; /1 is the default skip and therefore redundant, and that spec therefore runs once per minute due to the leading * instead of 0.)

0 10-15 * * * your command here

(I used 15, because it occurs to me that "between 10 and 4" is an exclusive range so you don't want to run at 16:00.)

0

If you want the script to be run every hour you can do something like this: [code] 00 10,11,12,13,14,15,16 * * * cd path_to_file; perl file.pl > path_to_logs/log.txt [/code]

This means when the minutes hit 00 and the hour hits any of 10 11 12 13 14 15 16 the script will be run

0

In your Perl script (or in a wrapper for the Perl script), you can use localtime to check the hour and exit if it isn't between 10am and 4pm:

use strict;
use warnings;

my @lt=localtime;
my $hour=$lt[2];

unless($hour>=10 and $hour<=16)
{
  print "Not between 10am and 4pm.  Exiting.\n";
  exit;
}

#put the rest of your code here.
1
  • Ur way is also correct. Its easy, fast and convenient. Thanks for your help. Appreciate it :)
    – Deepak
    Apr 23, 2012 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.