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

跟風遠走 提交于 2020-02-12 08:26:16

问题


I need to run a CRON job every 10 seconds from started time.

In Linux how to run a CRON job on every 10 seconds from the time its started?

I am trying to solve that as following: when I make a request (or start) at 04:28:34 it should start at 04:28:44 not at 4:28:40

This is what I have done

# m h  dom mon dow   command
*/10 * * * * /usr/bin/wget http://api.us/application/

What did I do wrong? Why does this not trigger wget every 10 seconds?


回答1:


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/ )  



回答2:


You have specified */10 in the minutes. Cron only allows for a minimum of one minute. You can try this -

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

Which will execute it in every 10 seconds with sleep.




回答3:


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



回答4:


Use watch instead; for example:

watch -n10 -x your_command


来源:https://stackoverflow.com/questions/30295868/how-to-setup-cron-job-to-run-every-10-seconds-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!