The standard cron
lacks second precision scheduling because
- Unix server administration traditionally rarely needed one-second precision
- It might be used in such a disturbing way for a multi-user system like "Run this task every second"
However with the help of sleep(1)
from GNU Coreutils suite, you can achieve true second precision job scheduling.
Example: wait for 12:14:05 and 12:14:10
$ crontab -l
(snip...)
14 12 * * * sleep 5; date > /tmp/plain.txt
14 12 * * * while [ "1410" -gt "$(date +\%M\%S)" ]; do /bin/sleep 0.1; done; date > /tmp/while.txt
(wait for a while...)
$ ls -l --time-style=full-iso /tmp/*.txt
-rw-r--r-- 1 nodakai nodakai 43 2014-02-22 12:14:06.236151567 +0800 /tmp/plain.txt
-rw-r--r-- 1 nodakai nodakai 43 2014-02-22 12:14:10.007600677 +0800 /tmp/while.txt
As you see from the 1st version, 14 12
in crontab
does not guarantee 12:14:00
sharp. The 2nd version uses while
loop and sub-second sleep(1)
to achieve sub-second precision.
Note that, unless you use NTP to synchronize your machine clock to time servers, it is meaningless to talk about second precision job scheduling.