Cronjobs can me made to run once a minute. Sometimes it is useful to run somehting in shorter intervals without installing additional software.
Table of contents
Sleep delays the execution of a command by the given amount of seconds. This way, it is possible to run a cronjob more frequently by delaying sequent executions:
* * * * * command * * * * * sleep 30; command
this will execute
command about every 30 seconds.
Limits and difficulties
The above method is clever and useful but has some limitations
The inaccuracy of sleep
sleep 1 is not guaranteed to wait for one second. It is guaranteed to wait
for at least one second. The delay mostly comes from launching the sleep
command itself, which takes time. The more often this is done, the bigger
the error appears.
Here is an overview of the execution time of sleeping 100 x 0.1 seconds: The tests have been done on similar hardware and are not accurate:
it’s important to point out that the BusyBox device, while being the fastest, had the lowest specs of all tested devices.
If something has to be executed really often, a fractional sleep time will be required. This might be a problem on some sleep implementations.
The POSIX definition of sleep defines the time Operand as follows:
A non-negative decimal integer specifying the number of seconds
Although most implementations support floating point numbers, some who exclusively implement the POSIX standard, will not.
It’s a viable solution to sleep for some seconds before executing a cronjob, in order to execute a cronjob more frequently than just once a minute.
The solution doesn’t scale well. I personally use it for a cronjob that needs to be executed every 10 seconds and it works fine with having 6 lines in my crontab.