需要执行shell_exec命令才能执行

问题描述:

我目前遇到了一个在我需要之前运行的shell命令的问题。例如,我有6个命令,我都想连续运行,前5个命令都很好,但是一旦我到达最后一个,它依赖于前一个产生的输出,它就不会运行。换句话说,无论如何,我可以让我的最后一个命令无法运行,直到第五个命令完全完成?这里快速查看一下我的代码片段,了解这种情况。需要执行shell_exec命令才能执行

 /*Renders user image*/ 
     //$command_1 = 'cd ./***/; povray +I'.$newname.' +O'.$newname_t.' +D '.$_POST['AA'].' +H75 +W100'; 
     $command_2 = 'cd ./***/; povray +I'.$newname.' +O'.$newname_i.' +D '.$_POST['AA'].' '.$_POST['resolution']; 

     /*Command to copy the .pov, .ini, and .inc files from User_Files to the correct animation directory before frame renders can be done*/ 
     $command_cp_pov = 'cp ***'.$newname.' ***'.$location; 
     $command_cp_ini = 'cp ***'.$newname_j.' ***'.$location; 
     $command_cp_inc = 'cp *** ***'.$location; 


     /*Render the frames for the animation*/ 
     $command_3 = 'cd ***'.$location.'; povray +I '.$newname_j.' +A +H384 +W512'.'> /dev/null 2>/dev/null &'; 

     /*Testing purposes*/ 
     //echo $command_3."\n"; 

     /*Throw together the animation using the .png files in the directory*/ 
     $command_4 = 'cd ***'.$location.'; convert -delay 0 -loop 0 *.png animation.gif'.'> /dev/null 2>/dev/null &'; 

     /*Testing purposes*/ 
     //echo $command_4; 

     $shellOutput = shell_exec($command_2); 
     $shellOutput = shell_exec($command_cp_pov); 
     $shellOutput = shell_exec($command_cp_ini); 
     $shellOutput = shell_exec($command_cp_inc); 
     $shellOutput = shell_exec($command_3); 
     // I want this command to run once the previous one is completely finished 
     $shellOutput = shell_exec($command_4); 

第五届了shell_exec命令的povray使用创建场景的50帧,所以我可以再使用的ImageMagick把所有这些帧汇集成GIF运行的.ini文件。它目前不能正常工作,因为我需要某种方式来延迟command_4的执行,直到command_3完全完成(所有50帧都被渲染)。

如果有人想知道所有的星号('*')是什么,那只是我不舒服,在我的服务器中显示我的实际位置。对不起,如果这混淆任何人。

+0

您可以检查进程是否在运行查看其进程ID – 2013-04-22 03:33:32

会是什么区别,如果你使用EXEC代替和测试更迭返回变量,然后运行lastone

/*Throw together the animation using the .png files in the directory*/ 

    /* can you change command to remain in the browser, 
     and report the results rather than writing to dev/null? */ 
    $command_3 = 'cd ***'.$location.'; povray +I '.$newname_j.' +A +H384 +W512'.'2>&1'; 


    exec($command_3,$shellOutput,$return_value); 
    // I want this command to run once the previous one is completely finished 
    if ($return_value === 0){ 
     $command_4 = 'cd ***'.$location.'; convert -delay 0 -loop 0 *.png animation.gif'.' 2>&1'; 

     exec($command_4,$shellOutput,$return_value); 
     if ($return_value !== 0){ 
       $failed_result = "An error on 4 ($return) occured.<br>"; 
       foreach($shellOutput as $v) 
        echo "shelloutput: $v<br>"; 
     } 
    } else { 
     $failed_result = "An error on 3 ($return) occured.<br>"; 
     foreach($shellOutput as $v) 
        echo "shelloutput: $v<br>"; 
    } 
+0

不幸的是,这并没有解决我的问题。我做了测试,但仍然得到了和以前相同的结果。我仍然得到了所有渲染的50帧,但.gif只是没有创建。 – Drew 2013-04-22 03:03:59

+0

尝试查看exec命令的结果。 – ndasusers 2013-04-22 03:19:55

+0

没关系,后来我想你可以在后面跑。目前,需要找出为什么它不写文件。 – ndasusers 2013-04-22 16:01:17

我不明白你使用的shell的语法。但是如果这样做有帮助的话,我从来没有遇到麻烦,因为在povray完成之前的大批量渲染之前,bash不耐烦,无法启动一个命令。我用的语法是这样的:在fracpos04

庆典

为我* .pov;做povray $ i + fn + w1280 + h960 + kff225 + a0.3;完成

你可以用PHP的Process API来实现这个结果。

如果你看一下proc_close功能,你会看到它明确规定:

proc_close()等待进程终止,并返回其退出代码。

proc_open manual page上有一个很好的例子。在执行期间,您可以通过调用proc_get_status来获取状态更新。