Home » Questions » Computers [ Ask a new question ]

How can I create a cron job that runs a task every three weeks?

How can I create a cron job that runs a task every three weeks?

I have a task that needs to be performed on my project schedule (3 weeks).

Asked by: Guest | Views: 73
Total answers/comments: 2
Guest [Entry]

"Thanks to earlier answers, have been pointed at the epoch options to date
-e or formatting as %s

Although a bit painful ((date +%s) / 86400) gives days from the epoch.

Relying on the weekly job being run at the same time, it is then easy to check this against a specific day of a 3 week period ($epoch_day%21 == 13 for instance)

In my case this is fine, as it is a one shot task. If it is missed on the particular day then there is no need to run at the next opportunity."
Guest [Entry]

"If you can save a timestamp file between runs, you can check the date of it instead of relying solely on the current date.

If your find command supports fractional values for -mtime (or has -mmin) (GNU find has both, POSIX does not seem to require either), you could ‘throttle’ the cron jobs with with find and touch.

Or, if you have a stat command that supports showing file dates as “seconds since the epoch” (e.g. stat from Gnu coreutils, also other implementations), you could make your own comparison using date, stat, and the shell's comparison operators (along with touch to update a timestamp file). You also might be able to use ls instead of stat if it can do the formatting (e.g. ls from GNU fileutils).

Below is a Perl program (I called it n-hours-ago) that updates a timestamp file and exits successfully if the original timestamp was old enough. Its usage text shows how to use it in a crontab entry to throttle a cron job. It also describes adjustments for “daylight savings” and how to handle ‘late’ timestamps from previous runs.

#!/usr/bin/perl
use warnings;
use strict;
sub usage {
printf STDERR <<EOU, $0;
usage: %s <hours> <file>

If entry at pathname <file> was modified at least <hours> hours
ago, update its modification time and exit with an exit code of
0. Otherwise exit with a non-zero exit code.

This command can be used to throttle crontab entries to periods
that are not directly supported by cron.

34 2 * * * /path/to/n-hours-ago 502.9 /path/to/timestamp && command

If the period between checks is more than one ""day"", you might
want to decrease your <hours> by 1 to account for short ""days""
due ""daylight savings"". As long as you only attempt to run it at
most once an hour the adjustment will not affect your schedule.

If there is a chance that the last successful run might have
been launched later ""than usual"" (maybe due to high system
load), you might want to decrease your <hours> a bit more.
Subtract 0.1 to account for up to 6m delay. Subtract 0.02 to
account for up to 1m12s delay. If you want ""every other day"" you
might use <hours> of 47.9 or 47.98 instead of 48.

You will want to combine the two reductions to accomodate the
situation where the previous successful run was delayed a bit,
it occured before a ""jump forward"" event, and the current date
is after the ""jump forward"" event.

EOU
}

if (@ARGV != 2) { usage; die ""incorrect number of arguments"" }
my $hours = shift;
my $file = shift;

if (-e $file) {
exit 1 if ((-M $file) * 24 < $hours);
} else {
open my $fh, '>', $file or die ""unable to create $file"";
close $fh;
}
utime undef, undef, $file or die ""unable to update timestamp of $file"";
exit 0;"