<bdo id='VtBGq'></bdo><ul id='VtBGq'></ul>
  • <small id='VtBGq'></small><noframes id='VtBGq'>

  • <tfoot id='VtBGq'></tfoot>

    <i id='VtBGq'><tr id='VtBGq'><dt id='VtBGq'><q id='VtBGq'><span id='VtBGq'><b id='VtBGq'><form id='VtBGq'><ins id='VtBGq'></ins><ul id='VtBGq'></ul><sub id='VtBGq'></sub></form><legend id='VtBGq'></legend><bdo id='VtBGq'><pre id='VtBGq'><center id='VtBGq'></center></pre></bdo></b><th id='VtBGq'></th></span></q></dt></tr></i><div id='VtBGq'><tfoot id='VtBGq'></tfoot><dl id='VtBGq'><fieldset id='VtBGq'></fieldset></dl></div>
  • <legend id='VtBGq'><style id='VtBGq'><dir id='VtBGq'><q id='VtBGq'></q></dir></style></legend>

      1. 如何使用 cron 作业安排动态功能?

        How to schedule dynamic function with cron job?(如何使用 cron 作业安排动态功能?)

        <small id='5qccD'></small><noframes id='5qccD'>

        1. <tfoot id='5qccD'></tfoot>
            <tbody id='5qccD'></tbody>
          <i id='5qccD'><tr id='5qccD'><dt id='5qccD'><q id='5qccD'><span id='5qccD'><b id='5qccD'><form id='5qccD'><ins id='5qccD'></ins><ul id='5qccD'></ul><sub id='5qccD'></sub></form><legend id='5qccD'></legend><bdo id='5qccD'><pre id='5qccD'><center id='5qccD'></center></pre></bdo></b><th id='5qccD'></th></span></q></dt></tr></i><div id='5qccD'><tfoot id='5qccD'></tfoot><dl id='5qccD'><fieldset id='5qccD'></fieldset></dl></div>
            1. <legend id='5qccD'><style id='5qccD'><dir id='5qccD'><q id='5qccD'></q></dir></style></legend>

                  <bdo id='5qccD'></bdo><ul id='5qccD'></ul>

                  本文介绍了如何使用 cron 作业安排动态功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道如何安排动态(自动填充数据)函数每天在保存的时间自动运行?

                  假设我有一个表单,一旦单击按钮,它就会将数据发送到发布数据的函数.我只是想让它自动化,这样我就不必按下按钮了.

                    <?phpforeach($Class->retrieveData as $data){<form method="post" action=""><li><input type="hidden" name="name">'.$data['name'].'<br/><input type="hidden" name="description">'.$data['description'].'<br/><input type="submit" name="post_data" value="Post"></表单>}?>

                  现在,表单将数据传递给函数.

                  if(isset($_POST['post_data']))//如果 post_data 按钮被点击,则它运行 myFunction(){我的功能();}我的函数(){$name = $_POST['name'];$description = $_POST['description'];}

                  我尝试执行以下操作,但问题是 Cron Job 只能运行整个 .php 文件,而我正在检索节省的时间以从 MySQL 运行.

                  foreach($Class->getTime() as $timeData){$timeHour = $timeData['timeHour'];$timeMinute = $timeData['timeMinute'];$hourMin = date('H:i');$timeData = ''.$timeHour.':'.$timeMinute.'';如果($hourMin == $timeData){运行我的功能.}}

                  $hourMin 是当前的小时:分钟,它与从 Mysql 自动运行的保存时间相匹配.所以如果 $hourMin == $timeData 那么函数就会运行.

                  如果 $hourMin 等于 $timeData,我如何运行 Cron 作业来自动运行 myFunction()?

                  所以...

                  List 1 = 将在上午 10 点运行列表 2 = 将在中午 12 点运行列表 3 = 将在下午 2 点运行

                  10am, 12pm, 2pm 是从 MySQL 检索的 $timeHour$timeMinute,但基于每个列表 ID.

                  编辑

                  @randomSeed,

                  1) 我可以安排 cron 作业.2) $name 和 $description 都将是数组,所以以下是我想要完成的.$name = 数组('乔恩','史蒂文','卡特');$描述 = 数组(乔恩是一个伟大的人.",'史蒂文有一个外向的性格.',卡特是一个可怕的人.");

                  如果预定时间正确,我想从 $name 和 $description 解析第一个数组.

                  在数据库中我有以下内容

                  postDataTime 表+----+---------+----------+------------+--------+|身份证 |timeDay |timeHour |时间分钟|邮编 |+--------------------------------------+--------+|1 |* |9 |0 |21 ||----|---------|----------|------------|--------||2 |* |10 |30 |22 ||----|---------|----------|------------|--------||3 |* |11 |0 |23 |+----|---------+---------+------------+--------+iD = 上传时自动递增.timeDay = * 是每天(cron 工作方式)timeHour = 一天中运行脚本的小时timeMinute = 运行脚本的小时数postiD = 这是位于另一个表中的帖子的 id(n+1 关系)

                  如果很难理解.. 什么是藜麦

                  if(time() == 10:30(time from MySQL postiD = 22)){//使用检索到的数据运行 myFunction,例如:$postiD = '22';$name = '史蒂文';$description = '史蒂文有一个外向的角色.';//以上是表单中 $_POST 中的内容,将是//发送到 myFunction()}

                  我只是想根据保存到 MySQL 的时间安排一切,如我在最顶部(postDataTime 表)所示.(我会展示我尝试过的东西,但我已经搜索了无数个小时来寻找我想要完成的示例,但我找不到任何东西,而且我尝试过的东西不起作用.)

                  我以为我可以使用 exec() 函数,但似乎不允许我运行函数,否则我会执行以下操作..

                  $time = '10:30';if($time == time()){exec(myFunction());}

                  解决方案

                  你有2种方法,尽管只有一种方法会完全按照你的意愿去做;>

                  • 第一种方式要求您有访问和特权来更改 cron-jobs 服务器端(例如通过 PHP 或其他方式).根据操作系统的不同,有教程:Win、尼克斯

                  • 第二种方式会做一些接近你想要的事情,但没有分钟精度,你每个周期最多会失去 2 分钟.(见下面的解释).

                  第一种方式完美的方式

                  • 一旦用户点击表单使用所需的数据时间为该用户创建一个独特的定时任务.
                  <块引用>

                  如果您没有这些权限,您可以使用 3d 零件服务,例如www.easycron.com 他们还提供免费版本询问.他们还提供了一个 REST API 方法来管理(CRUDE)定时任务.

                  第二种方式不完美的方式

                  • 添加一个新的 VARCHAR 列,我将其命名为 today,这样我们将确保该任务每天仅运行一次.

                  -

                  +----+---------+----------+------------+--------+------------+|身份证 |timeDay |timeHour |时间分钟|邮编 |今天 |+--------------------------------------+--------+----------+|1 |* |9 |0 |21 |30-05-04 ||----|---------|----------|------------|--------|----------+|2 |* |10 |30 |22 |||----|---------|----------|------------|--------|----------+|3 |* |11 |0 |23 ||+----|---------+---------+------------+--------+----------+

                  • 之后创建一个 php 文件,我将其命名为 crontask.php 我们将每 5 分钟调用一次

                  • 将此添加到您的 cronjob 面板:

                  • 0,5 * * * */usr/bin/php/www/virtual/username/crontask.php >/dev/null 2>&1

                  • crontask.php 文件中

                  -

                  功能说明:

                  假设 cron shedule 每 5 分钟运行一次,假设我们在 20:00 点,现在 cron 将在 20:05、20:10、20:15、20:20 等运行...

                  然后假设在我们的数据库中我们有这些时间

                  Jonh : 20:05,马里奥 : 20:32,卢克:20:48,大卫 : 20:57,吉米 : 20:06,涡流:20:16

                  当脚本检查这些时间时,它将运行如下:

                  在 20:05 ->运行 20:05 Jonh, 20:06 Jimmy在 20:10 ->运行空在 20:15 ->运行 20:16 涡流在 20:20 ->运行空等等....

                  如您所见,在最坏的情况下,每次都将失败2 分钟.我觉得够公平了!;)

                  I want to know how I can schedule a dynamic(auto populated data) function to auto run everyday at saved time?

                  Let's say I have a form that once the button is clicked it sends the data to the function, which the posts the data. I simply want to automate that so that I don't have to press the button.

                  <ul>
                      <?php 
                      foreach($Class->retrieveData as $data)
                      {
                          <form method="post" action="">
                              <li>
                                  <input type="hidden" name="name">'.$data['name'].'<br/>
                                  <input type="hidden" name="description">'.$data['description'].'<br/>
                                  <input type="submit" name="post_data"  value="Post">
                              </li>
                          </form>
                      }
                      ?>
                  </ul>
                  

                  Now, the form will pass the data to the function.

                  if(isset($_POST['post_data'])) // if post_data button is clicked then it runs myFunction()
                  {
                      myFunction();
                  }
                  
                  myFunction()
                  {
                      $name        = $_POST['name'];
                      $description = $_POST['description'];
                  }
                  

                  I tried doing the following but the problem is that Cron Job can only run the whole .php file, and I am retrieving the saved time to run from MySQL.

                  foreach($Class->getTime() as $timeData)
                  {
                      $timeHour    = $timeData['timeHour'];
                      $timeMinute = $timeData['timeMinute'];
                  
                      $hourMin    = date('H:i');
                      $timeData   = ''.$timeHour.':'.$timeMinute.'';
                  
                      if($hourMin == $timeData)
                      {
                          run myFunction.
                      }
                  }
                  

                  $hourMin is the current hour:minute which is being matched against a saved time to auto run from Mysql. So if $hourMin == $timeData then the function will run.

                  How can I run Cron Job to auto run myFunction() if the $hourMin equals $timeData?

                  So...

                  List 1 = is to be runned at 10am
                  List 2 = is to be runned at 12pm
                  List 3 = is to be runned at 2pm
                  

                  The 10am, 12pm, 2pm is the $timeHour and $timeMinute that is retrieved from MySQL but based on each list id's.

                  EDIT

                  @randomSeed,

                  1) I can schedule cron jobs.
                  2) $name and $description will all be arrays, so the following is what I am trying to accomplish.
                  
                  $name = array(
                      'Jon',
                      'Steven',
                      'Carter'
                  );
                  
                  $description = array(
                      'Jon is a great person.',
                      'Steven has an outgoing character.',
                      'Carter is a horrible person.'
                  );
                  

                  I want to parse the first arrays from both $name and $description if the scheduled time is correct.

                  In database I have the following

                  postDataTime table
                  
                  +----+---------+----------+------------+--------+
                  | iD | timeDay | timeHour | timeMinute | postiD |
                  +--------------------------------------+--------+
                  | 1  | *       | 9        | 0          | 21     |
                  |----|---------|----------|------------|--------|
                  | 2  | *       | 10       | 30         | 22     |
                  |----|---------|----------|------------|--------|
                  | 3  | *       | 11       | 0          | 23     |
                  +----|---------+----------+------------+--------+
                  
                  iD         = auto incremented on upload.
                  timeDay    = * is everyday (cron job style)
                  timeHour   = Hour of the day to run the script
                  timeMinute = minute of the hour to run script
                  postiD     = this is the id of the post that is located in another table (n+1 relationship)
                  

                  If it's difficult to understand.. what is quinoa

                  if(time() == 10:30(time from MySQL postiD = 22))
                  {
                      // run myFunction with the data that is retrieved for that time ex:
                  
                      $postiD = '22';
                      $name   = 'Steven';
                      $description = 'Steven has an outgoing character.';
                  
                      // the above is what will be in the $_POST from the form and will be
                      // sent to the myFunction()
                  }
                  

                  I simply want to schedule everything according to the time that is saved to MySQL as I showed at the very top(postDataTime table). (I'd show what I have tried, but I have searched for countless hours for an example of what I am trying to accomplish but I cannot find anything and what I tried doesn't work.).

                  I thought I could use the exec() function but from what it seems that does not allow me to run functions, otherwise I would do the following..

                  $time = '10:30';
                  if($time == time())
                  {
                      exec(myFunction());
                  }
                  

                  解决方案

                  you have 2 ways, although only one will do exactly what you want to do;

                  • 1st way requires that you have access and privileges to change cron-jobs server side (example via PHP or other). Depending on what OS there are tutorials: Win , Nix

                  • 2nd way will do something close to what you want but without the minutes precision, you will loose at max 2 minutes each cycle. (see exaplanation below).

                  1st Way perfect way

                  • As soon as the user hit the form create a unique cron-task for that user using the desired datatime.

                  if you don't have those privileges you can use 3d part service like www.easycron.com they also offer a Free version with limited query. they also provide a REST API method to manage (CRUDE) cron-tasks.

                  2nd Way imperfect way

                  • add a new VARCHAR column, i called it today with this we will ensure that the task will run only once per day.

                  -

                  +----+---------+----------+------------+--------+----------+
                  | iD | timeDay | timeHour | timeMinute | postiD |   today  |
                  +--------------------------------------+--------+----------+
                  | 1  | *       | 9        | 0          | 21     | 30-05-04 |
                  |----|---------|----------|------------|--------|----------+
                  | 2  | *       | 10       | 30         | 22     |          |
                  |----|---------|----------|------------|--------|----------+
                  | 3  | *       | 11       | 0          | 23     |          |
                  +----|---------+----------+------------+--------+----------+
                  

                  • after that create a php file i called it crontask.php we will call it each 5 minutes

                  • add this to your cronjob panel:

                  • 0,5 * * * * /usr/bin/php /www/virtual/username/crontask.php > /dev/null 2>&1

                  • in the crontask.php file

                  -

                  <?php
                  // include() Database Config file here with mysql_connect etc...
                  // include() the required files ex. the file where myFunction reside...
                  
                  $cron_cycle = 5; // set it equal to what used in cron command-line
                  $today = date('Y-m-d');
                  if($result = mysql_query("SELECT * FROM postDataTime WHERE today != '{$today}'")){
                      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
                          $postID = $row['postID'];
                          $timeHour = (int) $row['timeHour'];
                          $current_hours = (int) date('H'); // current hours
                          $current_minutes = (int) date('i'); // current minutes
                          $timeMinute = (int) $row['timeMinute'];
                          // force to run at the closest cycle
                          $timeMinute = ($timeMinute % $cycle === 0) ? $timeMinute : toCloser($timeMinute, $cron_cycle); 
                          if( $current_hours === $timeHour && $current_minutes === $timeMinute ){
                              // ensure that we have already runned a cron for this user...
                              mysql_query("UPDATE postDataTime SET today = '{$today}' WHERE postID = '{$postID}'");
                              myFunction($postID);
                          }
                      }
                  }
                  function toCloser($n,$x=5) {
                      $j = (round($n)%$x === 0) ? round($n) : (round(($n+$x/2)/$x)*$x);
                      return ($j-$n) >= round($x/2) ? ($j-$x) : $j;
                  }
                  
                  ?>
                  

                  Explanation of the function:

                  Assuming that the cron shedule runs each 5 minutes, lat's say we are at 20:00 o'clock, now the cron will run at 20:05, 20:10, 20:15, 20:20 and so on...

                  then assuming in our DB we have those time

                  Jonh  : 20:05, 
                  Mario : 20:32, 
                  luke  : 20:48, 
                  David : 20:57, 
                  Jimmy : 20:06, 
                  Eddy  : 20:16
                  

                  when the script checks against those times it will run as below:

                  at 20:05 -> run 20:05 Jonh, 20:06 Jimmy
                  at 20:10 -> run null
                  at 20:15 -> run 20:16 Eddy
                  at 20:20 -> run null
                  and so on....
                  

                  As you see you would loose in the worst case 2 minutes each time. I think it's fair enough! ;)

                  这篇关于如何使用 cron 作业安排动态功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)

                        <legend id='Hncgd'><style id='Hncgd'><dir id='Hncgd'><q id='Hncgd'></q></dir></style></legend>
                          <tbody id='Hncgd'></tbody>

                        <tfoot id='Hncgd'></tfoot>

                        <small id='Hncgd'></small><noframes id='Hncgd'>

                          <i id='Hncgd'><tr id='Hncgd'><dt id='Hncgd'><q id='Hncgd'><span id='Hncgd'><b id='Hncgd'><form id='Hncgd'><ins id='Hncgd'></ins><ul id='Hncgd'></ul><sub id='Hncgd'></sub></form><legend id='Hncgd'></legend><bdo id='Hncgd'><pre id='Hncgd'><center id='Hncgd'></center></pre></bdo></b><th id='Hncgd'></th></span></q></dt></tr></i><div id='Hncgd'><tfoot id='Hncgd'></tfoot><dl id='Hncgd'><fieldset id='Hncgd'></fieldset></dl></div>
                            <bdo id='Hncgd'></bdo><ul id='Hncgd'></ul>