Linux crontab Implementation-per-second instances

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, using delay to achieve every n seconds execution

Principle: The time delay method sleep N to implement every n seconds.

Creating a PHP execution action is as simple as writing the current time to log.

<? phpfile_put_contents ('/root/pengjun/php/crontab/run.log', date (' y-m-d h:i:s '). " \ r \ n " , file_append);? >

Enter the statement in the Linux crontab command, and then: Wq Save to exit.

* * * * * * php/home/fdipzone/php/crontab/tolog.php>>/tmp/date.txt; php/home/fdipzone/php/crontab/tolog.php>>/tmp/date.txt; php/home/fdipzone/php/crontab/tolog.php>>/tmp/date.txt; php/home/fdipzone/php/crontab/tolog.php>>/tmp/date.txt; php/home/fdipzone/php/crontab/tolog.php>>/tmp/date.txt; Php/home/fdipzone/php/crontab/tolog.php>>/tmp/date.txt

Using Tail-f to view the execution, you can see that log is written to a record every 10 seconds.

[Email protected] crontab]# tail-f/root/pengjun/php/crontab/run.log; -All:---- +:-£º11 :

Attention:

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. Write Shell script implementation

Principle: Use the For statement in SH to loop the specified number of seconds to execute.

#!/bin/Shstep=ten  #间隔的秒数, cannot be greater than0     $ ('/root/pengjun/crontab/ceshi.php')     0

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 is i<420,

*/7 * * * */home/fdipzone/php/crontab/crontab.sh

Linux crontab Implementation-per-second instances

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.