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

iBr*_*an2 10 php mysql cron cron-task

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

myFunction()
{
    $name        = $_POST['name'];
    $description = $_POST['description'];
}
Run Code Online (Sandbox Code Playgroud)

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

foreach($Class->getTime() as $timeData)
{
    $timeHour    = $timeData['timeHour'];
    $timeMinute = $timeData['timeMinute'];

    $hourMin    = date('H:i');
    $timeData   = ''.$timeHour.':'.$timeMinute.'';

    if($hourMin == $timeData)
    {
        run myFunction.
    }
}
Run Code Online (Sandbox Code Playgroud)

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

myFunction()如果$hourMin等于,我如何运行Cron Job自动运行$timeData

所以...

List 1 = is to be runned at 10am
List 2 = is to be runned at 12pm
List 3 = is to be runned at 2pm
Run Code Online (Sandbox Code Playgroud)

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

编辑

@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.'
);
Run Code Online (Sandbox Code Playgroud)

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

在数据库中,我有以下内容

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)
Run Code Online (Sandbox Code Playgroud)

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

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()
}
Run Code Online (Sandbox Code Playgroud)

我只想根据保存到MySQL的时间安排所有内容,就像我在最顶层显示的那样(postDataTime表).(我会展示我曾经尝试过的东西,但是我已经搜索了无数个小时的例子来说明我想要完成的事情,但我找不到任何东西,我尝试的东西也不起作用.)

我以为我可以使用exec()函数,但从它看起来不允许我运行函数,否则我会做以下..

$time = '10:30';
if($time == time())
{
    exec(myFunction());
}
Run Code Online (Sandbox Code Playgroud)

Bre*_*n F 7

Cron 任务要求您预设它们运行的​​时间,它们不能(是的,您可以通过编辑 crontab 的脚本来解决这个问题,但我不会说这是一个非常好的主意)动态决定它们的运行时间。这意味着您基本上有两个选择:

1)设置一个 cronjob 每分钟运行一次,并使用一个临时文件,您可以触摸该临时文件来告知它上次运行计划任务之一的时间。每次运行时,它都会检查在最后一个时间戳之间是否有任务要运行您的临时文件和当前时间,如果有则运行任务。这是一个粗暴但简单的解决方案。

2)不要使用cron。创建一个守护进程,检查任务需要运行的时间并将其放入优先级队列中,然后弹出最早的元素并休眠,直到运行该任务的时间为止。它运行该任务并将其重新插入以在未来 24 小时运行并重复。到目前为止,这个解决方案更加优雅,但也需要更多的工作。


Luc*_*ofi 3

你有两种方法,尽管只有一种能完全满足你的要求;

  • 第一种方法要求您具有更改 cron-jobs 服务器端的访问权限和权限(例如通过 PHP 或其他)。根据操作系统的不同,有教程:WinNix

  • 第二种方法会做一些接近你想要的事情,但如果没有分钟精度,你将在每个周期最多 2 分钟丢失。(参见下面的解释)。

第一种方式完美的方式

  • 一旦用户点击表单,就使用所需的数据时间为该用户创建一个唯一的 cron 任务。

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

第二种方法不完美

  • 添加一个新VARCHAR列,我用它来命名它,today我们将确保该任务每天仅运行一次。

-

+----+---------+----------+------------+--------+----------+
| iD | timeDay | timeHour | timeMinute | postiD |   today  |
+--------------------------------------+--------+----------+
| 1  | *       | 9        | 0          | 21     | 30-05-04 |
|----|---------|----------|------------|--------|----------+
| 2  | *       | 10       | 30         | 22     |          |
|----|---------|----------|------------|--------|----------+
| 3  | *       | 11       | 0          | 23     |          |
+----|---------+----------+------------+--------+----------+
Run Code Online (Sandbox Code Playgroud)
  • 之后创建一个我称之为的 php 文件,我们每5分钟crontask.php调用一次

  • 将其添加到您的 cronjob 面板中:

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

  • crontask.php在文件中

-

<?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;
}

?>
Run Code Online (Sandbox Code Playgroud)

函数说明:

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

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

Jonh  : 20:05, 
Mario : 20:32, 
luke  : 20:48, 
David : 20:57, 
Jimmy : 20:06, 
Eddy  : 20:16
Run Code Online (Sandbox Code Playgroud)

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

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....
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,在最坏的情况下您每次都会损失2 分钟。我想这已经足够公平了!;)