du8589840 2015-02-10 08:38
浏览 55
已采纳

cron job不会激活php脚本,生成错误

I have a simple script which currently sends an email to 3 persons.

If I go directly to the url, the script works normally ( it sends the emails ).

However, if I set it up as a cron job in cpanel, I get the following notice to my cron email when the cron job fires:

/home/ultranet/public_html/newsletter/blast_script.php: line 1: ?php: No such file or directory
/home/ultranet/public_html/newsletter/blast_script.php: line 2: /*: No such file or directory
/home/ultranet/public_html/newsletter/blast_script.php: line 3: Script: command not found
/home/ultranet/public_html/newsletter/blast_script.php: line 4: January: command not found
/home/ultranet/public_html/newsletter/blast_script.php: line 5: Please: command not found
/home/ultranet/public_html/newsletter/blast_script.php: line 6: ----------------------------------------------------------------------: command not found
/home/ultranet/public_html/newsletter/blast_script.php: line 7: include_once: command not found
/home/ultranet/public_html/newsletter/blast_script.php: line 8: syntax error near unexpected token `('
/home/ultranet/public_html/newsletter/blast_script.php: line 8: `$sql = mysql_query("SELECT * FROM newsletter WHERE received='1' LIMIT 20");'

and the script is:

<?php
/* -------------------------------------------------------------------
Script written by Adam Khoury @ www.developphp.com
                          January 1, 2010
Please retain this credit when displaying this code online 
---------------------------------------------------------------------- */
include_once "connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM newsletter WHERE received='1' LIMIT 20");
$numRows = mysql_num_rows($sql); // Added for "End Campaign Check" at the bottom of this file(not shown on the video)
$mail_body = '';
while($row = mysql_fetch_array($sql)){
    $id = $row["id"];
    $email = $row["email"];
    $name = $row["name"];

    $mail_body = '<html>
<body style="background-color:#CCC; color:#000; font-family: Arial, Helvetica, sans-serif; line-height:1.8em;">
<h3><a href="http://www.developphp.com"><img src="http://www.yoursite.com/images/logo.png" alt="DevelopPHP" width="216" height="36" border="0"></a> Newsletter
</h3>
<p>Hello ' . $name . ',</p>
<p>You can make this out to be just like most any web page or design format you require using HTML and CSS.</p>
<p>~Adam @ DevelopPHP</p>
<hr>
<p>To opt out of receiving this newsletter,  <a href="http://www.developphp.com/Tests/newsletter/optout.php?e=' . $email . '">click here</a> and we will remove you from the listing immediately.</p>
</body>
</html>';
    $subject = "Develop PHP Newsletter";
    $headers  = "From:newsletter@developphp.com
";
    $headers .= "Content-type: text/html
";
    $to = "$email";

    $mail_result = mail($to, $subject, $mail_body, $headers);

    if ($mail_result) {
        // mysql_query("UPDATE newsletter SET received='1' WHERE email='$email' LIMIT 1");
    } else {
       // this else statement can be used to write into an error log if the mail function fails
       // It can also be removed if you do not need error logging
    }

}

// This section is script I discussed adding to this file on video
// This section is for sending the site owner a message informing them that
// all people in the database have been sent the newsletter for the current campaign
if ($numRows == 0) { // $numRows is set on line 4 using the existing query

     $subj = "Newsletter Campaign Has Ended";
     $body = "The current newsletter campaign has ended. All have been sent the newsletter.";
     $hdr  = "From:newsletter@developphp.com
";
     $hdr .= "Content-type: text/html
";
     mail("yourEmailAddressHere", $subj, $body, $hdr);

}
// End Check Section
?>

Why does it generate errors, since the script works normally when directly called in the url?

I asked my host but they don't know what the problem is.

  • 写回答

3条回答 默认 最新

  • douba8048 2015-02-10 08:45
    关注

    I guess you just set the cronjob's command to /path/to/your/script.php, But this script is not an executable. You have to prefix this script with the php interpreter, i. e.

    php /path/to/your/script.php

    in the cronjob's configuration.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongpu1908 2015-02-10 08:50
    关注

    You have to specify the path to php at the begining of each line who execute a php script. For exemple in debian in the crontab (crontab -e):

    01 00 * * * /usr/bin/php5 /var/www/Projet/script.php
    
    评论
  • dongmi4035 2015-02-10 08:50
    关注

    write down on command line

     which php 
    

    It will return the path of the file, may be something like

     /etc/bin/php
    

    Instead of calling php directly in the cron use it as

    /etc/bin/php /path/to/script.php
    

    Php is not set in the environment that is why cron is not able to find php.

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Google Chrome 所有页面崩溃,三种解决方案都没有解决,我崩溃了
  • ¥18 如何用c++编写数学规律题
  • ¥20 使用uni-app发起网络请求,获取重定向302返回的cookie
  • ¥20 手机外部浏览器拉起微信小程序支付 (相关搜索:微信小程序)
  • ¥20 怎样通过一个网址找到其他同样模版的网址
  • ¥30 XIAO esp32c3 读取FDC2214的数据
  • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信
  • ¥15 关于PROCEDURE和FUNCTION的问题
  • ¥100 webapi的部署(标签-服务器)
  • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)