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 seconds
Creating a PHP execution action is as simple as writing the current time to log.

<?  file_put_contentsdate(' y-m-d h:i:s '). " \ r \ n ",


CRONTAB-E Enter the following statement, and then: Wq Save to exit.

* * * * * *php/home/fdipzone/php/crontab/sleep; php/home/fdipzone/php/crontab/sleep; php/home/fdipzone/php/crontab/sleep; php/home/fdipzone/php/crontab/sleep; php/home/fdipzone/php/crontab/sleep


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

[Email protected]:~$Tail-f/home/fdipzone/php/crontab/Run.log the-Geneva- to  +: -: on  the-Geneva- to  +: -: One  the-Geneva- to  +: -: +  the-Geneva- to  +: -: to  the-Geneva- to  +: -: A  the-Geneva- to  +: -:Wuyi  the-Geneva- to  +: -: on 


Principle: The time delay method sleep N to implement every n seconds.
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. Writing a shell script implementation
crontab.sh

#!/bin/Bash Step=2for0 /home/fdipzone/php/crontab/tolog.php '   sleep done 0

CRONTAB-E Enter the following statement, and then: Wq Save to exit.

* * * * * */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.

[Email protected]:~/php/crontab$Tail-F Run.log the-Geneva- to  A: at: on the-Geneva- to  A: at:Geneva the-Geneva- to  A: at: . the-Geneva- to  A: at: , the-Geneva- to  A: at:Ten the-Geneva- to  A: at: A the-Geneva- to  A: at: - the-Geneva- to  A: at: - the-Geneva- to  A: at: - the-Geneva- to  A: at: - the-Geneva- to  A: at: A the-Geneva- to  A: at: - the-Geneva- to  A: at: - the-Geneva- to  A: at: in the-Geneva- to  A: at: to the-Geneva- to  A: at: - the-Geneva- to  A: at: * the-Geneva- to  A: at:Panax Notoginseng the-Geneva- to  A: at: the the-Geneva- to  A: at: A the-Geneva- to  A: at: - the-Geneva- to  A: at: $ the-Geneva- to  A: at: - the-Geneva- to  A: at: - the-Geneva- to  A: at: the the-Geneva- to  A: at: Wu the-Geneva- to  A: at: About the-Geneva- to  A: at: - the-Geneva- to  A: -:xx

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

The value of crontab.sh step is 7, the loop end condition i<420, CRONTAB-E can enter the following statement to achieve

# m H  Dom Mon Dow   Command*/7 * * * */home/fdipzone/php/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.