简体   繁体   English

CRON无法正常运作

[英]CRON isn't working

I have the following php code set to run as a CRON job. 我将以下php代码设置为作为CRON作业运行。 It runs once a day and never returns an error so I assume it is running properly. 它每天运行一次,从不返回错误,因此我认为它运行正常。 The problem is, the rows aren't being deleted from my database! 问题是,这些行没有从我的数据库中删除!

I've tested the php and the variables work. 我已经测试了php和变量工作。 I've tested my query and that works too so I'm at a loss... 我已经测试过我的查询,它也可以正常工作,所以我很茫然。

<?php
$isCLI = ( php_sapi_name() == 'cgi-fcgi' );

if (!$isCLI)
    die("cannot run! sapi_name is ".php_sapi_name());
exit;

//Connect to DB
mysql_connect("xxxxxx.com", "xxxxxxxxxx", "xxxxxxxxxxxx") or die(mysql_error());
mysql_select_db("xxxxxxxxxxx") or die(mysql_error());

//Get today's date
$todayis = date("Y-m-d");
$todayis = strtotime(date("Y-m-d", strtotime($todayis)) . " -4 hours");
$todayis = date('Y-m-d', $todayis);

//Delete rows in userContests where reminder is less than or equal to today's date
mysql_query("DELETE FROM userContests WHERE reminder <= '$todayis';") or die(mysql_error());
?>

Can someone explain to me why the rows won't delete? 有人可以向我解释为什么行不会删除吗?

If that is the whole script, I would say you have forgotten to connect to the database. 如果这是整个脚本,那么我会说您忘记了连接到数据库。

Edit: This seems to be the problem: 编辑:这似乎是问题所在:

if (!$isCLI)
    die("cannot run! sapi_name is ".php_sapi_name());
exit;

That translates to: 转换为:

if (!$isCLI) 
{
    die("cannot run! sapi_name is ".php_sapi_name());
}

exit;

So basically you always stop your script on the 6th line, nothing after that will ever be executed. 因此,基本上,您总是在第六行停止脚本,此后将不再执行任何操作。

If it is a CRON job, it should be using PHP-CLI not PHP-CGI . 如果是CRON作业,则应使用PHP-CLI而不是PHP-CGI You can add this to the top of the file to do the right check. 您可以将其添加到文件顶部以进行正确的检查。

<?php if(PHP_SAPI != 'cli') throw new Exception('Not in CLI mode!');

...

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

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