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