邮件脚本需要每小时运行一次,但是cron不能正常工作

时间:2017-04-01 04:47:53

标签: java php cron sleep

以下是功能完备的代码。当我通过浏览器调用它时,它工作。但是,我无法让我的Cron工作来运行它。我已经尝试过提出任何解决方案,并且愿意接受任何想法,这会使每个小时24/7 365一次点火这个脚本。

的信息:

我尝试了以下内容:

在执行任何此类操作时,我还没有将其添加到数据库中或发送一封电子邮件:

  • /opt/php54/bin/php /home1/user/public_html/Sales/scripts/sales-notif_em.php - 无法打开输入文件:/home1/user/public_html/scripts/sales-notif_em.php
  • php /home1/user/public_html/Sales/scripts/sales-notif_em.php - 空白电子邮件,但显示内容:内容类型:text / html
  • php -q /home1/lotscav1/public_html/Sales/scripts/sales_notif_em.php - 我当然没有从系统收到任何电子邮件
  • /usr/bin/curl home1/user/public_html/scripts/sales-notif_em.php - 卷曲:(3)格式错误
  • 任何具有http://website.c0m/Sales/scripts/sales_notif_em.php后半部分的命令(如php或/ usr / bin / curl) - 在尝试添加Cron作业时导致JSON错误

我检查了以下内容:

  • 文件权限为655
  • 在customers表
  • 下,时间戳不到一小时

这是我的代码:

<?php
//find out current time and 1 hour ago
date_default_timezone_set('America/New_York');
$current_time = strtotime("now");
$hour_ago = strtotime('-1 hour');

//////////////////////////////////////////////////////////////////
/////////////////l// Connect to Sales Database  ///////////////////
//////////////////////////////////////////////////////////////////

$mysqli_s = new mysqli("localhost", "user", "password", 
"server_sales_data");
if ($mysqli_s->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli_s->connect_errno . ") 
" . $mysqli_s->connect_error;
}

//////////////////////////////////////////////////////////////////
///////////////////// Connect to EM Database  ////////////////////
//////////////////////////////////////////////////////////////////

$mysqli_em = new mysqli("localhost", "user", "password", 
"server_dlgEM");
if ($mysqli_em->connect_errno) {
echo "Failed to connect to MySQL_EM: (" . $mysqli_em->connect_errno . 
") " . $mysqli_em->connect_error;
}

//Grab store name
$dlg_store = "EM";

$em_request = "SELECT * FROM customers WHERE date BETWEEN '$hour_ago' 
AND '$current_time'";
$em_result = mysqli_query($mysqli_em, $em_request) or die("Error No 
Sales EM");
while ($em_row = mysqli_fetch_array($em_result)) {
$em_prod_num = $em_row["prod_num"];
$em_receipt = $em_row["receipt"];


//////////////////////////////////////////////////////////////////
///////////////////// Grab info for EM Sales  ////////////////////
//////////////////////////////////////////////////////////////////

$request_s = "SELECT * FROM all_products WHERE 
dlgprod_num='$em_prod_num' AND dlg_store='$dlg_store'";
$result_s = mysqli_query($mysqli_s, $request_s) or die("Error dlg 
prod num EM");
while ($row_s = mysqli_fetch_array($result_s)) {
        $sku_s = $row_s["sku"];
        $dlgprod_num_s = $row_s["dlgprod_num"];
        $book_title_s = addslashes($row_s["book_title"]);
        $dlgprod_price_s = $row_s["dlgprod_price"];
        $author_name_s = addslashes($row_s["author_name"]);
        $author_email_s = $row_s["author_email"];
        $publisher_s = $row_s["publisher"];
        $dlg_store_s = $row_s["dlg_store"];

            $add_sql_s  = "INSERT INTO all_author_sales SET
            `sku`='$sku_s', 
            `dlgprod_num`='$dlgprod_num_s',
            `dlgprod_nam`='$book_title_s',
            `dlgprod_price`='$dlgprod_price_s',
            `author_name`='$author_name_s',
            `author_email`='$author_email_s',
            `publisher`='$publisher_s',
            `dlg_store`='$dlg_store_s',
            `dlgcustomer_receipt`='$em_receipt' ";  

//create signature
$sig = "The Admin Team at www.website.com";
//to
$admin_email = "admin@website.com";
$to = array($author_email_s, $admin_email);

//setup email headers
$headers='From: ' . $admin_email . "\r\n" .
'Reply-To: ' . $admin_email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= $emailbody."\n\n";

//email subject and body
$subject = "Your book stats";
$message = "
Hi $author_name_s,<br />
I just wanted to send you a message and let you know that the book or 
books below have just been purchased.<br /><br />

Store: $dlg_store_s<br />
Receipt: $em_receipt<br />
Sku Number: $sku_s<br /><br />

Book Title: $book_title_s<br />
Publisher: $publisher_s<br />
Product Number: $dlgprod_num_s<br />
Price: $dlgprod_price_s<br /><br />

Sincerely,<br />
$sig<br /><br />

To remove yourself from this notification, please send an email to 
$admin_email with Unsubscribe in the subject line.
";

            if ($mysqli_s->multi_query($add_sql_s) === TRUE) {
            mail (implode(',', $to), $subject, $message, $headers); 
            } else {
                echo "Error: " . $add_sql_s . "<br>" . $mysqli_s-
>error . "<br>" . $string;
            }


    }



}


?>

2 个答案:

答案 0 :(得分:0)

我打赌你要执行的php文件的路径错误。

如果可以,请尝试使用终端,导航到放置文件的文件夹,然后尝试通过命令行运行它。

还要介意您正在引用两个不同的名称:sales-notif_em.php&amp;销售notif_me.php。

答案 1 :(得分:0)

(代表OP发布)

感谢您的帮助。我用我的电子邮件作为测试目的的作者制作了一个自定义产品。在客户表中我插入了产品111111和111110.但是,在all_products中我给了产品11111111和11111110.tHE最终结果是我犯了错误。

我希望你能从这次活动中学到一些东西。知识比我的自我更重要。谢谢!