2015-02-10 61 views
1

我有一個簡單的腳本,目前發送電子郵件給3人。cron作業不會觸發工作的PHP腳本,生成錯誤

如果我直接轉到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:[email protected]\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:[email protected]\r\n"; 
    $hdr .= "Content-type: text/html\r\n"; 
    mail("yourEmailAddressHere", $subj, $body, $hdr); 

} 
// End Check Section 
?> 

它爲什麼會產生錯誤,因爲直接在url中調用腳本時腳本正常工作?

我問我的主人,但他們不知道問題是什麼。

+0

聽起來像PHP可執行文件無法找到..在常規的crontab中,例如,您會添加PATH =/bin:/ usr/bin:/ usr/local/bin。 – Oli 2015-02-10 08:42:45

+0

查看我的解決方案 – 2015-02-10 08:51:18

+0

我的主持人告訴我要從字面上加上以下內容:/home/ultranet/public_html/newsletter/blast_script.php – 2015-02-10 09:01:47

回答

3

我想你只是將cronjob的命令設置爲/path/to/your/script.php,但是這個腳本不是可執行文件。你必須在php腳本中添加前綴。即

php /path/to/your/script.php

中的cronjob的配置。

+0

我的主持人告訴我要從字面上加入以下內容:/home/ultranet/public_html/newsletter/blast_script.php ,所以我應該做的PHP主頁/ ultranet/public_html /時事通訊/ blast_script.php?它開頭還是其他的只是「php」? – 2015-02-10 09:02:41

+0

如何在不詢問主機的情況下找到php的路徑? – 2015-02-10 09:09:40

+0

當php可以在您的服務器的路徑中找到應該是所有的。如果不檢查Danyal Sandeelo的解決方案。如果您的服務器上沒有shell訪問權限,則必須向您的提供者詢問php(cli)二進制文件的完整路徑。 – 2015-02-10 09:11:54

0

你必須在執行php腳本的每一行的開頭指定php的路徑。 對於爲例Debian中在crontab(crontab -e命令):

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

寫在命令行

which php 

它將返回文件的路徑, 可能類似於

/etc/bin/php 

不是直接在cron中調用php,而是使用它作爲

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

Php未在環境中設置,因此cron無法找到php。

+0

哪裏可以寫哪個php?如何在不詢問主機的情況下找到php的實際路徑? – 2015-02-10 09:08:25

+0

@lliccaMindstorm在linux命令行中寫下哪個php,它會告訴你php的實際路徑 – 2015-02-10 09:15:50

+0

@OlliccaMindstorm讓我知道你是否遇到過任何錯誤 – 2015-02-10 09:19:35