0

我昨天设置的 cron 作业有一个奇怪的问题。我正在运行以下 CronJob 脚本:

0 7 * * * php -q /wocs/email.php

这指向我放在下面的电子邮件脚本。目标是让它运行调用我的 MySQL 数据库的脚本并向用户发送一封电子邮件,说明他们必须签署多少票。当我在 phpmyadmin 中运行查询时,我得到了正确的结果,当我在页面上回显结果时,它给了我正确的结果。但是今天早上 7 点,Cron Job 给数据库中的每个用户发送了电子邮件(即使是没有任何未完成票证的用户 - 所以他们根本不应该收到任何东西,当我测试查询时他们不会出现我自己运行脚本)。他们每个人都收到了十几次电子邮件,他们收到的号码不准确,我无法复制自己。

这是代码,当我在浏览器中运行它时它工作得很好,我唯一得到这个错误的时候是运行 Cron Job 时。

$query="
SELECT *, COUNT(*)
FROM job, user
WHERE job.jobs_osuper=user.users_id
AND job.jobs_approverid2 = 0
GROUP BY user.users_id
";              

$result=mysql_query($query);

mysql_close();

$num=mysql_num_rows($result);
                    $conditional1=mysql_result($result,$i,"users_id");
                    $conditional2=mysql_result($result,$i,"jobs_osuper");

$i = 0;
while($i<$num && $conditional1 == $conditional2)
{
                            $name=mysql_result($result,$i,"user_fullname");
                            $email=mysql_result($result,$i,"user_email");
                            $count=mysql_result($result,$i,"COUNT(*)");

$mailfrom = "from@email.com";
$mailbcc = "bcc@email.com";
$message = "<b>$name,</b><p>You have $count ticket(s) waiting on your approval.  <a href='http://url.com'>Click here to review the tickets assigned to you.</a></p>";

mail($email, "Tickets for Approval", $message, "From: $mailfrom\r\nCC: $mailbcc\r\nContent-type: text/html\r\n");
$i++;
}
4

1 回答 1

0

Your script is likely being run by a different user through the cron job. I'm not sure whether this will affect your result, but it has caused me grief in the past.

Your cron command should look something like this:

sudo su www-data -c "php -q /wocs/email.php"

Make sure that www-data is the correct user - you can check by running echo get_current_user(); from the browser.

于 2013-01-27T23:44:42.197 回答