2013-04-27 86 views
0

我撕裂我的頭髮試圖找出爲什麼當它在命令行中執行時,該工作可能可能無法正常工作。在完成渲染並使用ImageMagick將幀編譯爲GIF後,我向用戶發送POV-Ray動畫。我已經通過mutt sleep發送了一個小時的電子郵件,因此它允許編譯用戶動畫的時間。 (我把它改成了120秒,但是我不必等一個小時才能排除故障,在這方面我也讓動畫只有3幀,直到我找出問題所在。)Mutt的郵件不發送電子郵件了shell_exec

新的bash文件animation_done_bash.sh是每次用戶點擊我的站點上的動畫時創建的,而該動畫創建一個新的文件夾,用於存儲動畫的框架以及animation_done_bash.sh。爲了使PHP頁面前進到下一頁,bash文件在dev null中執行。我已經測試過,如果這是在命令行上的問題,它在那裏工作,所以我真的不知道是什麼問題。

下面是代碼和正在用了shell_exec執行輸出(I呼應了這一點,在我的PHP頁面,看看發生了什麼事)所產生:

$animation_done_email = "#!/usr/bin/bash \nsleep 120; mutt -s \"Animation Finished\" -a animation.gif -- ".$email." <email.txt;"; 

     /*Creates new animation_done_bash each time user hits animate*/ 
     $directory_bash = "./User_Files/Animation/".$location."/"; 
     $filename_bash = 'animation_done_bash.sh'; 
     $handler_bash = fopen($directory_bash.$filename_bash, "w"); 
     fwrite($handler_bash, $animation_done_email); 
     fclose($handler_bash); 

     /*Need to change the permissions on bash file so it can execute*/ 
     chmod($directory_bash."/animation_done_bash.sh", 0777); 

     $command_5 = 'cd /home/ouraccount/public_html/User_Files/Animation/'.$location.'/; bash animation_done_bash.sh > /dev/null 2>/dev/null &'; 
     $shellOutput = shell_exec($command_5); 

其中$ email是用戶的電子郵件$ location是存儲框架的文件夾。 email.txt存儲在同一個文件夾中。

輸出: CD /家庭/ ouraccount /的public_html/User_Files /動畫/ ani_51 /;的bash animation_done_bash.sh>的/ dev/null的2>的/ dev/null的&

任何指導將不勝感激。謝謝!

+1

我想你會以錯誤的方式進行操作,只需在bash腳本中添加一條命令,以便在完成渲染後發送圖像。 – dtech 2013-04-27 12:45:17

+0

這就是我想要做的。我無法弄清楚它在完成渲染時如何識別,所以我只是讓電子郵件發送睡眠。如果這不是正確的方法,我應該用什麼來代替mutt?我只是困惑,爲什麼它一切工作,當我從外殼執行它,但不是當它從PHP執行,即使我改變了權限。 – Dominick 2013-04-27 13:21:07

+0

也許是因爲PHP暫停或用戶不正確或其他原因。太多的可能性,它只是不正確的PHP使用情況。我將在下面發佈一個示例代碼片段。這是一個XY問題:http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – dtech 2013-04-27 14:02:28

回答

1

在這種推(調用腳本時,渲染操作完成)的情況下是preferrable輪詢(定期檢查,如果渲染操作完成)。

如果你不能推,這樣做只用一種語言,不要創建bash和PHP的混合體。 下面是2個例子,你可以嘗試這可能會適合你的情況:

如果渲染命令返回後整理舉例:

<?php 
    /** PHP wrapper around rendering command X that mails the output **/ 
    // Don't write attachment code yourself, use a class like PHPMailer: https://github.com/Synchro/PHPMailer 
    require_once('class.phpmailer.php'); 

    // Ignore user browser close, rendering takes a long time 
    ignore_user_abort(true); 
    // On windows you'll also need to set_time_limit to something large. On Unix PHP doesn't count things like database queries and shell commands, on Windows it does 

    // Execute render command, don't forget to escape if you use user input 
    // Script will only continue once renderer returns. If renderer return before rendering is finished you cannot use this 
    $render_output = shell_exec('renderer input.file output.file'); 
    // Could also be done in PHP for finer control and error handling 
    $imagemagick_output = shell_exec("convert output.file animation.gif"); 
    unlink("output.file"); 

    $mail = new PHPMailer(); 
    $mail->addAttachment('animation.gif'); 
    // etc. 

    unlink("animation.gif"); 
    ?> 

如果實例完成之前渲染命令返回:

<?php 
    /** PHP wrapper around rendering command X that mails the output **/ 
    // Don't write attachment code yourself, use a class like PHPMailer: https://github.com/Synchro/PHPMailer 
    require_once('class.phpmailer.php'); 

    // Ignore user browser close 
    ignore_user_abort(true); 

    // Execute render command, don't forget to escape if you use user input 
    // If renderer returns before file is finished use this 
    $render_output = shell_exec('renderer input.file output.file 2> error.file'); 

    // Wait for rendering to finish 
    // Implement these function, e.g. file_exists for output.file or error.file 
    while(!rendering_has_failed() && !rendering_is_finished()) { 
     sleep(15*60); // Low-resource wait for 15 minutes 
    } 

    // Could also be done in PHP for finer control and error handling 
    $imagemagick_output = shell_exec("convert output.file animation.gif"); 
    unlink("output.file"); 

    $mail = new PHPMailer(); 
    $mail->addAttachment('animation.gif'); 
    // etc. 

    unlink("animation.gif"); 
    ?> 
+0

或者只是創建一個shell腳本在後臺執行; '渲染;兌換; mutt' – tripleee 2013-04-27 14:51:08

+0

@tripleee這也是一個不錯的選擇,但鬼過程的可能性增加了,而且更難調試錯誤。 – dtech 2013-04-27 14:58:18

+0

謝謝大家!我會考慮未來。現在我發送了一個鏈接到動畫,而不是給用戶。從我可以看到,雖然上述解決方案將與我試圖完成的工作。謝謝你的幫助! – Dominick 2013-04-27 16:18:01