2013-04-27 88 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