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
Menu
Cloud Tag
Categories
- Announcements (1)
- Benchmarks (2)
- How-To (5)
- News (1)
- Projects (1)
- Tips (5)
Archive
- June 2011 (1)
- May 2011 (1)
- January 2011 (1)
- October 2010 (1)
- August 2010 (4)
- July 2010 (1)



