How precise is a cron daemon?

Solution 1:

What cron can guarantee is that your job will start no sooner than the specified time (subject to precision of the system clock). But there is no way to give you any guarantee about the termination time of the job. It will depend on a lot of factors:

  • How loaded is the system
  • What does the job do
  • Slowness due to hardware issues
  • Slowness due to networking issues (assuming the job depends on networking)

My recommendation is to change your design such that a specific completion time is not a requirement.

Solution 2:

I suppose this depends on your cron daemon, but the documentation and the standard indicate that if you specify minutes, the job will execute at the specified minute.

See:

  • The crontab documentation
  • SUSv2 on crontab and the crontab file format

Be aware that your script will be started when the clock ticks over to the correct time, but will finish some time after that.

Solution 3:

Usually cron will start at 23:59:00, scan all your crontab files, filter out those who are relevant for 23:59 and then start them. Scanning this files is very fast, because there are not many of them and they all only include a few lines. So usually, the cronjobs start at 23:59:00 or 23:59:01 There are sure ways to intentionally slow down this process. (add millions of lines to the crontab, for example). If the system is totally overloaded, this also won't work that fast.

Also, this is obviously implementation dependent.

If you need very exact start times, you are better off creating a programm that sleeps until the time you want and then runs (e.g. using c++11). But on a non real-time OS, this also won't be exact! Also the clock of the PC doesn't know the exact time!

In all cases, this only makes sure the program starts at (more or less) the time you want. There cannot be any guarantee that the program ends successful until a given time, so I strongly believe you should change something on that requirement.

Solution 4:

It depends from your overall script execution time and precision of server time.

59 23 * * * /some/script/file.sh

will launch your script exactly at 23:59, but, if you have some commands which works a long time, part of script may be executed after midnight.