Linux crontab Implementation per second

Source: Internet
Author: User

The Linux crontab command, the minimum execution time is one minute. If you need to repeat within less than a minute, there are two ways to implement it. 1. Use delay to implement every n secondsCreating a PHP execution action is as simple as writing the current time to log. <?php file_put_contents ('/home/fdipzone/php/crontab/run.log ', date (' y-m-d h:i:s '). "   \ r \ n ", file_append); ?> CRONTAB-E Enter the following statement, and then: Wq Save to exit.
* * * * * * php/home/fdipzone/php/crontab/tolog.php * * * * * * sleep 10; php/home/fdipzone/php/crontab/tolog.php * * * * * * sleep 20; php/home/fdipzone/php/crontab/tolog.php * * * * * * sleep 30; php/home/fdipzone/php/crontab/tolog.php * * * * * * sleep 40; php/home/fdipzone/php/crontab/tolog.php * * * * * * sleep 50; php/home/fdipzone/php/crontab/tolog.php
Using Tail-f to view the execution, you can see that log is written to a record every 10 seconds.
Fdipzone @ubuntu: ~$ tail-f/home/fdipzone/php/crontab/run.log 2014-03-31 21:47:01 2014-03-31 21:47:11 2014-03-31 21:47: 21 2014-03-31 21:47:31 2014-03-31 21:47:41 2014-03-31 21:47:51 2014-03-31 21:48:01
Principle: The time delay method sleep N to implement every n seconds.   Note: 60 must be divisible by the number of seconds (no remainder), such as the number of seconds in the interval is 2,4,6,10,12. If the number of seconds in the interval is too small, such as 2 seconds to execute, you need to add the 60/2=30 clause to the crontab. This method is not recommended and can be used in the second method described below. 2. Writing a shell script implementation
crontab.sh #!/bin/bash step=2 #间隔的秒数, cannot be greater than (i = 0; i <; i= (I+step))); Do $ (php '/home/fdipzone/php/crontab/tolog.php ') sleep $step done exit 0
CRONTAB-E Enter the following statement, and then: Wq Save to exit. # m H Dom Mon Dow Command * * * * */home/fdipzone/php/crontab/crontab.sh

Using Tail-f to view the execution, you can see that log is written to a record every 2 seconds.
Fdipzone @ubu NTU: ~/php/crontab$ tail-f run.log 2014-03-31 22:23:01 2014-03-31 22:23:03 2014-03-31 22:23:06 2014-03-31 22:23:08 2014-0 3-31 22:23:10 2014-03-31 22:23:12 2014-03-31 22:23:14 2014-03-31 22:23:16 2014-03-31 22:23:18 2014-03-31 22:23:20 2014-03- 31 22:23:22 2014-03-31 22:23:25 2014-03-31 22:23:27 2014-03-31 22:23:29 2014-03-31 22:23:31 2014-03-31 22:23:33 2014-03-31 22:23:35 2014-03-31 22:23:37 2014-03-31 22:23:39 2014-03-31 22:23:41 2014-03-31 22:23:44 2014-03-31 22:23:46 2014-03-31 2 2:23:48 2014-03-31 22:23:50 2014-03-31 22:23:52 2014-03-31 22:23:54 2014-03-31 22:23:56 2014-03-31 22:23:58 2014-03-31 22: 24:00
Principle: Use the For statement in SH to loop the specified number of seconds to execute. Note: If 60 does not divide the number of seconds between intervals, you need to adjust the execution time.   For example, you need to execute every 7 seconds, you need to find 7 and 60 least common multiple, 7 and 60 least common multiple is 420 (that is, 7 minutes). Then the value of crontab.sh step is 7, the loop end condition i<420, CRONTAB-E can enter the following statement to implement # M H Dom Mon Dow command */7 * * */home/fdipzone/p hp/crontab/crontab.sh

Linux crontab Implementation per second

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.