How to get Crond to perform tasks in seconds

Source: Internet
Author: User

Yesterday wrote a requirement of the application script, according to the actual needs of the best can make it automatically every 3 seconds, but Crond seems to only support the points, how to do?


The first method:


Of course, the first thought is to write a trigger script that uses a dead loop in the trigger script to solve the problem, as follows:

Cat kick.sh

--------

#!/bin/bash

While:;d o

/home/somedir/scripts.sh 2>/dev/null &

Sleep 3

Done

-----------

Note Do not use Bash kick.sh & this way of running in the background for the first time, it will be zombie.

You can put it on a scheduled task to run it, and then delete this entry in the scheduled task. Finally put this script into/etc/rc.local so that it can be run every time it is powered on.


The second method:


Similar to the first method, but feels more convenient than the first.

Cat cron-seconds.sh

------------

#!/bin/bash

#For excuting The scripts every 3 seconds in Crond.

#20100124. WXG


For ((i=1;i<=20;i++));d o

/home/somedir/scripts.sh 2>/dev/null &

Sleep 3

Done

----------------

Then write the crontab every minute to execute, as follows

Crontab-e

--------

* * * * * */bin/bash/home/somedir/cron-seconds.sh

---------------


The third method:


So how do you use scheduled tasks to implement them directly?

The final solution is as follows, and it is verified that the script runs very stably.


Crontab-e

---------------------

# for Excuting scripts.sh every 3 seconds# #on 2010-01-22

* * * * * */home/somedir/scripts.sh

* * * * * * Sleep 3 &&/home/somedir/scripts.sh

* * * * * * Sleep 6 &&/home/somedir/scripts.sh

* * * * * * Sleep 9 &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep Wuyi &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

* * * * * * sleep &&/home/somedir/scripts.sh

#-----------------------------------------------------------------


If you think about it, you can understand the truth.


I'm still more inclined to use the third method. Because the first method and the second method is not strictly interval 3 seconds execution, will be greater than 3 seconds, because the execution of scripts.sh also takes a certain amount of time, even if the & symbol has been added to the background execution will also have some error. If the accuracy requirement is not high, the second method is recommended.


How to let Crond perform a task in seconds

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.