简体   繁体   中英

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. I added the path to the feedUpdate.php file and 1 0 * * * to execute it every minute. The feedUpdate.php looks like this:

<?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.

To run every minute, the cron job schedule should be * * * * * .

1 0 * * * means to execute every day at 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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