Matthew
Matthew

Reputation: 6609

Will using ntpd possibly mess up cron jobs?

My system has a lot of time drift, after a couple of week it can be ahead by around 5 minutes.

To fix this, I installed ntp, which can help fix drift and of course sync the time to the ntp servers occasionally.

Suppose I have a cron job for something like backing up files. Is the following scenario possible?

02:00:00 - Cron starts a job
02:00:03 - Ntpd syncs time back 5 seconds to 01:59:58
02:00:00 - 2:00 gets repeated and cron job runs twice

If the cron job copies large files, the second run might try overwriting files the first job is not finished writing to.

Thanks!

Upvotes: 3

Views: 1398

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263267

Here's what the cron(8) man page says (this is for Vixie cron; other implementations might behave differently):

Special considerations exist when the clock is changed by less than 3 hours, for example at the beginning and end of day‐ light savings time. If the time has moved forwards, those jobs which would have run in the time that was skipped will be run soon after the change. Conversely, if the time has moved backwards by less than 3 hours, those jobs that fall into the repeated time will not be re-run.

Only jobs that run at a particular time (not specified as @hourly, nor with '*' in the hour or minute specifier) are affected. Jobs which are specified with wildcards are run based on the new time immediately.

Clock changes of more than 3 hours are considered to be corrections to the clock, and the new time is used immediately.

Upvotes: 3

jim mcnamara
jim mcnamara

Reputation: 16379

ntp.conf allows you to control how often ntp connects to the time server to make corrections. The minimum is 64 seconds by default (minpoll) and the default for maxpoll is 1024 seconds. see man ntp.conf

Therefore, if you correct time manually at a time when it will not impact things, then start ntp, the amount of drift is going to be small, very much subsecond changes.

One of our m4000's has a clock like that, and we never have had a problem.

Note: The only problem with a lock file is that if the backup severely abends no more backups will run until the lock file is reset.

Upvotes: 0

Karlson
Karlson

Reputation: 3048

In this particular scenario it might but I would suggest that you put a locking mechanism into the backup job that it will only allows it to run once:

In PERL you can do the following:

flock("/tmp/backup-running", LOCK_EX) || die "I'm already running";

While shell may not provide you a similar mechanism except by running:

touch /tmp/backup-running
if [ -f "/tmp/backup-running" ]
then
    exit 0;
fi

....


/bin/rm /tmp/backup-running

which is prone to problems other languages provide you with similar facilities as PERL.

Upvotes: 1

Related Questions