Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup CRON job to run every 10 seconds in Linux?

People also ask

How do I run a cron job every 5 seconds?

Cron only allows for a minimum of one minute. What you could do is write a shell script with an infinite loop that runs your task, and then sleeps for 5 seconds. That way your task would be run more or less every 5 seconds, depending on how long the task itself takes.

How do I run a cron job every second?

“cron run every second” Code Answer's*/10 * * * * * will run every 10 sec.


To elaborate on Sougata Bose's answer, I think the OP wants a command to be run every 10 seconds from a start time; not 10 seconds after the first minute and every subsequent minute.

cron only has a resolution of 1 minute (there are other tools I think that may have finer resolutions but they are not standard on unix).

Therefore, to resolve your issue you need 60 seconds / 10 seconds = 6 cron jobs, each with a sleep.

e.g. run crontab -e and add the following lines to your chosen editor:

* * * * * ( /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 10 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 20 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 30 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 40 ; /usr/bin/wget http://api.us/application/ )  
* * * * * ( sleep 50 ; /usr/bin/wget http://api.us/application/ )  

Another option is to edit your crontab with crontab -e and write:

* * * * * for i in {1..6}; do /usr/bin/wget http://api.us/application/ & sleep 10; done

*/10 * * * * will run every 10 min.
*/10 * * * * * will run every 10 sec.

You can checkout the cron editor for more options.


Using commas in seconds field works too:

0,10,20,30,40,50 * * * * *