简体   繁体   English

Cron 作业在 5 天后更新行

[英]Cron job to update row after 5 days

I'm having difficulty to make a cron job on cpanel to update the state of an user if the account has been created for 5 days.如果帐户已创建 5 天,我很难在 cpanel 上进行 cron 作业以更新用户的 state。 It just doesn't work at all.它根本不起作用。 When I program the cron job on the cpanel it does nothing.当我在 cpanel 上对 cron 作业进行编程时,它什么也不做。 I even tested it with minutes, but doesn't work.我什至用几分钟测试了它,但没有用。

Here's my table on database:这是我的数据库表:

CREATE TABLE `users_tmp` (
  `idUsers` int(11) NOT NULL,
  `ipUser` varbinary(16) NOT NULL,
  `uidUsers` longtext COLLATE utf8_unicode_ci NOT NULL,
  `emailUsers` longtext COLLATE utf8_unicode_ci NOT NULL,
  `pwdUsers` longtext COLLATE utf8_unicode_ci NOT NULL,
  `dataUser` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `stateUser` longtext COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

And here's my code to update (update_row.php):这是我要更新的代码(update_row.php):

<?php
require 'dbh.inc.php';


$sql = "UPDATE users_tmp SET stateUser = 'Inactive' WHERE timestamp < NOW() - INTERVAL 5 DAY;


Create a shell script and use this for the cron job.创建一个 shell 脚本并将其用于 cron 作业。

No need for php at all完全不需要 php

#!/bin/bash

mysql --user=[username] --password=[password] --database=[db name] --execute="UPDATE users_tmp SET stateUser = 'Inactive' WHERE timestamp < NOW() - INTERVAL 5 DAY;"

ALso there are events in mysql mysql 中也有事件

Enable it with启用它

SET GLOBAL event_scheduler = ON;

and create an event like this:并创建一个这样的事件:

CREATE EVENT name_of_event
ON SCHEDULE EVERY 1 DAY
STARTS '2020-04-19 00:00:00'
DO
UPDATE users_tmp SET stateUser = 'Inactive' WHERE timestamp < NOW() - INTERVAL 5 DAY;

If I understand correctly your sql query is already working on your environment and you wish to create a cron job on linux to call your page which is made in PHP!如果我理解正确,您的 sql 查询已经在您的环境中运行,并且您希望在 linux 上创建一个 cron 作业来调用您的 PHP 页面!

If above is the correct scenario, then you first need to make sure the sql script is executed within your cron_script.php file as follows:如果上述情况正确,那么您首先需要确保在您的 cron_script.php 文件中执行 sql 脚本,如下所示:

<?php
require 'dbh.inc.php';    
// Let's assume that you have an active connection to your database by now
$sql = "UPDATE users_tmp SET stateUser = 'Inactive' WHERE timestamp < NOW() - INTERVAL 5 DAY";
if (mysqli_query($conn, $sql)) {
    // createCronLog is an optional function that you can create and use to make a log of what has happened on your cron activity!
    createCronLog("Record updated successfully");
} else {
    createCronLog("Error updating record: " . mysqli_error($conn));
}
mysqli_close($conn);
?>  

Now if you wish to place the above file into let's say your home directory: "/home/yourusername/path/to/cron/cron_script.php" somewhere outside the public_html (so that it can be only called from your cron job and not from www).现在,如果您希望将上述文件放入您的主目录: "/home/yourusername/path/to/cron/cron_script.php" public_html 之外的某个地方(这样它只能从您的 cron 作业中调用,而不是来自 www)。

Here is your cron job command:这是您的 cron 作业命令:

/usr/local/bin/php /home/yourusername/path/to/cron/cron_script.php

Here is the cron job command and activity call once/per/hour这是 cron 作业命令和活动调用一次/每小时

0   *   *   *   *  /usr/local/bin/php /home/yourusername/path/to/cron/cron_script.php

If your file has dependencies such as $_GET or $_POST variables, you may put the cron_script.php file somewhere inside your public_html folder and call your cron file using a full path (with domain) for the crob to call your script:如果您的文件具有 $_GET 或 $_POST 变量等依赖项,您可以将 cron_script.php 文件放在 public_html 文件夹中的某个位置,并使用完整路径(带域)调用您的 cron 文件,以便 crob 调用您的脚本:

0   *   *   *   *  /usr/local/bin/php https://yourdomain.tld/path/to/cron/cron_script.php

I have tested the above and it works with in linux environment on centos 7 running cPanel.我已经测试了上述内容,它适用于 centos 7 运行 cPanel 的 linux 环境。 But it must also work on whatever environment your setup is, as long as your cron job is executing correctly and calling your cron_script.php file.但它也必须适用于您的设置的任何环境,只要您的 cron 作业正确执行并调用您的 cron_script.php 文件。

PS: Note that there is a space between "/usr/local/bin/php" AND "/home/yourusername/path/to/cron/cron_script.php" PS:注意"/usr/local/bin/php""/home/yourusername/path/to/cron/cron_script.php"之间有一个空格

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

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