Aug 11, 2010
Usu

How to run a cron job every X seconds

There’s a really simple way for doing this directly in crontab: putting the process to sleep.

Let’s see how: imagine you need to run a PHP script every 10 seconds to check whether a webiste is responding, here’s what you would need to put in crontab:

* * * * * /usr/local/bin/php /var/www/uptime.php
* * * * * sleep 10; /usr/local/bin/php /var/www/uptime.php
* * * * * sleep 20; /usr/local/bin/php /var/www/uptime.php
* * * * * sleep 30; /usr/local/bin/php /var/www/uptime.php
* * * * * sleep 40; /usr/local/bin/php /var/www/uptime.php
* * * * * sleep 50; /usr/local/bin/php /var/www/uptime.php

What we’re doing here, is starting 6 processes altogether every minute with progressive sleep times and that results in running the PHP script every 10 seconds. As simple as that.

Leave a comment