简体   繁体   English

您如何使用cron作业使HTML页面内的PHP脚本每分钟重复一次?

[英]How do you use a cron job to make a PHP script inside an HTML page repeat every minute?

I have my HTML page with a few PHP scripts inside, I would like one of those scripts to repeat every minute. 我的HTML页面中包含一些PHP脚本,我希望其中一个脚本每分钟重复一次。 I created a new "minute.php" file and I would like to make it call the script every minute. 我创建了一个新的“ minute.php”文件,我想让它每分钟调用一次脚本。 I don't get in which file I have to put the script that I want to run every second, in the original HTML page or in the "minute.php" file. 我没有将必须每秒运行的脚本放入原始HTML页面或“ minute.php”文件中,而没有进入该文件中。 Where do I put the * * * * * ... line ? 我在哪里放置* * * * * ...行?

If you are expecting the change in HTML after each minute use setInterval and not Cron Job. 如果您希望每分钟之后HTML发生变化,请使用setInterval而不是Cron Job。

Call jQuery Ajax Request Each X Minutes 每X分钟调用jQuery Ajax请求

You can use the built-in javascript setInterval. 您可以使用内置的javascript setInterval。

 var ajax_call = function() { //your jQuery ajax code }; var interval = 1000 * 60 * X; // where X is your every X minutes setInterval(ajax_call, interval); 

or if you are the more terse type ... 或者如果您是更简洁的类型...

 setInterval(function() { //your jQuery ajax code }, 1000 * 60 * X); // where X is your every X minutes 

ajax_call above can be ajax call to minute.php. 上面的ajax_call可以是对minute.php的ajax调用。

You could reside your minute.php script in any place you desire. 您可以将minute.php脚本驻留在minute.php的任何位置。 Like /var/www/site/cron/minute.php for example. 例如/var/www/site/cron/minute.php

To execute this script you may execute binary php program and pass it the path to the script as argument like the following: 要执行此脚本,您可以执行二进制php程序并将其路径作为参数传递给脚本,如下所示:

$ /path/to/php /var/www/site/cron/minute.php

To run this command every minute you have to add the following record in crontab : 要每分钟运行此命令,必须在crontab添加以下记录:

* * * * * /path/to/php /var/www/site/cron/minute.php

Note that the path to the script should be absolute. 请注意,脚本的路径应为绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM