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

The thought

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:

implementation runtime
GNU 0m10.257s
BusyBox 0m10.170s
OpenBSD 0m11.00s
Mac 0m10.330s

it’s important to point out that the BusyBox device, while being the fastest, had the lowest specs of all tested devices.

POSIX sleep

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

The Open Group Base Specifications Issue 7, 2018 edition

Although most implementations support floating point numbers, some who exclusively implement the POSIX standard, will not.

Conclusion

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.