0

我正在撕裂我的头发,试图弄清楚为什么当它从命令行执行时它可能不起作用。在使用 ImageMagick 完成渲染并将帧编译为 GIF 后,我将向用户发送 POV-Ray 动画。我通过 mutt sleep 发送了一个小时的电子邮件,以便有时间编译用户动画。(不过,我已将其更改为 120 秒,这样我就不必等待一个小时来排除故障。在这方面,我还让动画只有 3 帧,直到我找出问题所在。)

每次用户在我的网站上单击 animate 时,都会创建一个新的 bash 文件,animation_done_bash.sh,该文件又会创建一个新文件夹,用于存储动画的帧,其中包含 animation_done_bash.sh。为了使 PHP 页面前进到下一页,bash 文件在 dev null 中执行。我已经测试过这是否是命令行上的问题并且它在那里工作,所以我真的不知道是什么问题。

下面是使用 shell_exec 执行的代码和输出(我将其回显到我的 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 /home/ouraccount/public_html/User_Files/Animation/ani_51/;bash animation_done_bash.sh > /dev/null 2>/dev/null &

任何指导将不胜感激。谢谢!

4

1 回答 1

1

在这种情况下,推送(渲染操作完成时调用脚本)优于轮询(定期检查渲染操作是否完成)。

如果您无法推送,请仅使用一种语言进行推送,不要创建 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");
   ?>
于 2013-04-27T14:29:43.273 回答