dsf6281 2014-08-22 00:07
浏览 69

shell_exec导致无限循环

I have a problem with shell_exec. I try to run the other php file in a separate thread, according to this answer: https://stackoverflow.com/a/222445/1999929 I have this very-very simple code:

<?php
     $realpath = realpath("./second.php");
     file_put_contents("./log.txt","
First php running!
",FILE_APPEND);
     shell_exec("php $realpath > /dev/null 2>/dev/null &");
?>

I need this because i want to use this file for a dropbox webhook link, and it has to give a response in 10 seconds, while processing the changed files sometimes takes more time. So this file has to tell te other to run, and give a response, while not waiting for the other to finish.

When shell_exec is used in the code, the text is outputted infinite times in the file, without it its working fine, but i need to call the other file somehow.

EDIT - I tried exec() too, because the answer above used it instead of shell_exec, results are the same.

  • 写回答

2条回答 默认 最新

  • donglie7268 2016-11-27 20:33
    关注

    The issue is with the ENV, something in the ENV conflict with the second call to PHP making PHP calling infinite times the second file. This will create a fork bomb.

    But if you just reset the $env array to an empty array the second file will be called correctly.

    Neither shell_exec() or exec() all you to manipulate $ENV. You'll need to use "proc_open":

    resource proc_open ( $cmd , $descriptorspec , &$pipes [, $cwd [, $env]] )
    

    So:

    <?php
         $env = array();
         $realpath = realpath("./second.php");
         file_put_contents("./log.txt","
    First php running!
    ",FILE_APPEND);
         proc_open(
            "php $realpath > /dev/null 2>/dev/null &",
            $descriptorspec,
            $pipes,
            __DIR__,
            $env
         );
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺