【问题标题】:cron job doesn't fire working php script, generates errorscron 作业不会触发工作 php 脚本,会产生错误
【发布时间】:2015-02-10 08:38:50
【问题描述】:

我有一个简单的脚本,目前正在向 3 个人发送电子邮件。

如果我直接访问 url,脚本可以正常工作(它会发送电子邮件)。

但是,如果我在 cpanel 中将其设置为 cron 作业,当 cron 作业触发时,我会在我的 cron 电子邮件中收到以下通知:

/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");'

脚本是:

<?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\r\n";
    $headers .= "Content-type: text/html\r\n";
    $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\r\n";
     $hdr .= "Content-type: text/html\r\n";
     mail("yourEmailAddressHere", $subj, $body, $hdr);

}
// End Check Section
?>

直接在url中调用脚本正常工作,为什么会报错?

我问过我的主人,但他们不知道问题出在哪里。

【问题讨论】:

  • 听起来好像找不到 PHP 可执行文件。例如,在常规 crontab 中,您可以添加 PATH=/bin:/usr/bin:/usr/local/bin。
  • 我的主人告诉我按字面意思输入以下内容:/home/ultranet/public_html/newsletter/blast_script.php

标签: php cron


【解决方案1】:

我猜你只是将 cronjob 的命令设置为/path/to/your/script.php,但是这个脚本不是可执行文件。您必须在这个脚本前面加上 php 解释器,即。 e.

php /path/to/your/script.php

在 cronjob 的配置中。

【讨论】:

  • 我的主机告诉我按字面意思输入以下内容: /home/ultranet/public_html/newsletter/blast_script.php ,所以我应该做 php home/ultranet/public_html/newsletter/blast_script.php?真的只是开头的“php”还是别的什么?
  • 如何在不询问主机的情况下找到php的路径?
  • 当 php 可以在您的服务器路径中找到时,应该就是全部了。如果没有,请查看 Danyal Sandeelo 的解决方案。如果您在服务器上没有 shell 访问权限,则必须向您的提供商询问 php (cli) 二进制文件的完整路径。
【解决方案2】:

在命令行上写下来

 which php 

它会返回文件的路径, 可能类似于

 /etc/bin/php

不要直接在cron中调用php,而是使用它作为

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

环境中没有设置php,这就是cron找不到php的原因。

【讨论】:

  • 我在哪里写哪个php?如何在不询问宿主的情况下找到php的实际路径?
  • @OlliccaMindstorm on linux命令行..写下哪个php,它会告诉你php的实际路径
  • @OlliccaMindstorm 如果您遇到任何错误,请告诉我
  • 我无法访问 ssh,我将不得不与我的提供商打交道。
【解决方案3】:

您必须在执行 php 脚本的每一行的开头指定 php 的路径。 以 debian 中的 crontab 为例(crontab -e):

01 00 * * * /usr/bin/php5 /var/www/Projet/script.php

【讨论】:

    猜你喜欢
    • 2011-05-10
    • 2016-12-06
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 2017-03-09
    相关资源
    最近更新 更多