Linux crontab implementation repeats in less than one minute

Source: Internet
Author: User
Tags exit current time sleep linux

Linux crontab command, the minimum execution time is one minute. If you need to repeat in less than one minute, there are two ways to implement it.

1. Use delay to implement every n seconds

Create a PHP to do the action, very simple, is to write the current time 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 exit.

* * * * * * * * * * * * * * * * * * php/home/fdipzone/php/crontab/tolog.php * * * * * * * *  
* * * * * sleep 20; php/home/fdipzone/php/crontab/tolog.php * * * * * * * * * * * * * * * * * * * * * php/home/fdipzone/php/crontab/tolog.php * * * *  
EEP 40; php/home/fdipzone/php/crontab/tolog.php * * * * * * * * sleep  
; 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  
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: Through the 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 between intervals is 2,4,6,10,12.

If the number of seconds in the interval is too small, such as 2 seconds to execute, you will need to add the 60/2=30 statement in Crontab. Using this method is not recommended, you can use the second method described below.

2. Write the Shell script implementation

crontab.sh

#!/bin/bash  
      
step=2 #间隔的秒数, cannot be greater than (  
      
(i = 0; i < i= (i+step)); do  
    $ (php '/home/fdipzone/php/cronta B/tolog.php ') sleep  
    $step  
done  
      
exit 0

CRONTAB-E Enter the following statement, and then: Wq save 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@ubuntu:~/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-03-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 22: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: In SH use for statement implementation loop specifies the number of seconds to execute.

Note: If 60 cannot divide the number of seconds between partitions, you need to adjust the time of execution. For example, if you need to perform every 7 seconds, you need to find 7 and 60 LCM, and 7 and 60 LCM 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 the

# m H  Dom Mon Dow   Command */7 * * * *  
/home/fdipzone/php/crontab/crontab.sh

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

Related Article

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.