简体   繁体   English

使用cron作业每分钟将新行插入表中

[英]Insert new row into table every minute with cron job

I am trying to run a cron job every minute that will insert a new row into a specific table every minute. 我正在尝试每分钟运行一次cron作业,该作业将每分钟将新行插入到特定表中。 I added the path to the feedUpdate.php file and 1 0 * * * to execute it every minute. 我将路径添加到feedUpdate.php文件,并添加了1 0 * * *以每分钟执行一次。 The feedUpdate.php looks like this: feedUpdate.php看起来像这样:

<?php
require_once("../php_includes/db_conx.php");

$sql = "SELECT * FROM posts ORDER BY RAND() LIMIT 1"; //Select a random row from 'posts'
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
$id = $row['id']; //get the id of that specific random post selected
$sqlins = "INSERT INTO postfeed (postid, time) VALUES ('$id',now())"; //Insert the new post id into the feed table.
$queryins = mysqli_query($db_conx, $sqlins);
?>

This script works fine when I just write the path directly in the browser, but it doesn't run automatically every minute. 当我直接在浏览器中直接编写路径时,此脚本可以很好地工作,但它不会每分钟自动运行。

Any ideas why? 有什么想法吗?

I am using 000webhost at the moment as it's free, but I know for a fact that it does run cron jobs as I have one that runs daily clearing temporary files. 我目前正在使用000webhost,因为它是免费的,但我知道它确实运行cron作业,因为我有一个每天运行的作业清除临时文件。

To run every minute, the cron job schedule should be * * * * * . 要每分钟运行一次,cron作业时间表应为* * * * *

1 0 * * * means to execute every day at 00:01 . 1 0 * * *表示每天00:01执行。

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

To run every minute try this: 要每分钟运行一次,请尝试以下操作:

* * * * * php /path/to/file/runScript.php


Stackoverflow: How can I write a PHP cron script? Stackoverflow: 如何编写PHP cron脚本?

Mirror: http://iswwwup.com/t/7cb7527ceffc/how-can-i-write-a-php-cron-script.html 镜像: http//iswwwup.com/t/7cb7527ceffc/how-can-i-write-a-php-cron-script.html

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

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