• <i id='rJhoi'><tr id='rJhoi'><dt id='rJhoi'><q id='rJhoi'><span id='rJhoi'><b id='rJhoi'><form id='rJhoi'><ins id='rJhoi'></ins><ul id='rJhoi'></ul><sub id='rJhoi'></sub></form><legend id='rJhoi'></legend><bdo id='rJhoi'><pre id='rJhoi'><center id='rJhoi'></center></pre></bdo></b><th id='rJhoi'></th></span></q></dt></tr></i><div id='rJhoi'><tfoot id='rJhoi'></tfoot><dl id='rJhoi'><fieldset id='rJhoi'></fieldset></dl></div>

      1. <small id='rJhoi'></small><noframes id='rJhoi'>

        <legend id='rJhoi'><style id='rJhoi'><dir id='rJhoi'><q id='rJhoi'></q></dir></style></legend>

      2. <tfoot id='rJhoi'></tfoot>

          <bdo id='rJhoi'></bdo><ul id='rJhoi'></ul>
      3. PHP exec() 后台进程的返回值 (linux)

        时间:2023-10-30

            <bdo id='nYD9f'></bdo><ul id='nYD9f'></ul>

          • <i id='nYD9f'><tr id='nYD9f'><dt id='nYD9f'><q id='nYD9f'><span id='nYD9f'><b id='nYD9f'><form id='nYD9f'><ins id='nYD9f'></ins><ul id='nYD9f'></ul><sub id='nYD9f'></sub></form><legend id='nYD9f'></legend><bdo id='nYD9f'><pre id='nYD9f'><center id='nYD9f'></center></pre></bdo></b><th id='nYD9f'></th></span></q></dt></tr></i><div id='nYD9f'><tfoot id='nYD9f'></tfoot><dl id='nYD9f'><fieldset id='nYD9f'></fieldset></dl></div>
          • <small id='nYD9f'></small><noframes id='nYD9f'>

              <legend id='nYD9f'><style id='nYD9f'><dir id='nYD9f'><q id='nYD9f'></q></dir></style></legend>
                <tbody id='nYD9f'></tbody>
                • <tfoot id='nYD9f'></tfoot>
                • 本文介绍了PHP exec() 后台进程的返回值 (linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Linux 上使用 PHP,我想确定使用 exec() 运行的 shell 命令是否成功执行.我正在使用 return_var 参数来检查成功的返回值 0.这工作正常,直到我需要对必须在后台运行的进程执行相同的操作.例如,在以下命令中 $result 返回 0:

                  Using PHP on Linux, I'd like to determine whether a shell command run using exec() was successfully executed. I'm using the return_var parameter to check for a successful return value of 0. This works fine until I need to do the same thing for a process that has to run in the background. For example, in the following command $result returns 0:

                  exec('badcommand > /dev/null 2>&1 &', $output, $result);
                  

                  我故意将重定向放在那里,我不想捕获任何输出.我只想知道命令执行成功了.有可能吗?

                  I have put the redirect in there on purpose, I do not want to capture any output. I just want to know that the command was executed successfully. Is that possible to do?

                  谢谢,布赖恩

                  推荐答案

                  我的猜测是,您尝试做的事情不可能直接实现.通过后台处理进程,您可以让 PHP 脚本在结果存在之前继续(并可能退出).

                  My guess is that what you are trying to do is not directly possible. By backgrounding the process, you are letting your PHP script continue (and potentially exit) before a result exists.

                  一种解决方法是使用第二个 PHP(或 Bash/etc)脚本来执行命令并将结果写入临时文件.

                  A work around is to have a second PHP (or Bash/etc) script that just does the command execution and writes the result to a temp file.

                  主脚本类似于:

                  $resultFile = '/tmp/result001';
                  touch($resultFile);
                  exec('php command_runner.php '.escapeshellarg($resultFile).' > /dev/null 2>&1 &');
                  
                  // do other stuff...    
                  
                  // Sometime later when you want to check the result...
                  while (!strlen(file_get_contents($resultFile))) {
                      sleep(5);
                  }
                  $result = intval(file_get_contents($resultFile));
                  unlink($resultFile);
                  

                  command_runner.php 看起来像:

                  $outputFile = $argv[0];
                  exec('badcommand > /dev/null 2>&1', $output, $result);
                  file_put_contents($outputFile, $result);
                  

                  它并不漂亮,当然还有增加健壮性和处理并发执行的空间,但总体思路应该可行.

                  Its not pretty, and there is certainly room for adding robustness and handling concurrent executions, but the general idea should work.

                  这篇关于PHP exec() 后台进程的返回值 (linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP - 长时间运行的后台任务 下一篇:PHP中是否有等效的Java HashMap?

                  相关文章

                • <legend id='XmC3S'><style id='XmC3S'><dir id='XmC3S'><q id='XmC3S'></q></dir></style></legend>
                    <bdo id='XmC3S'></bdo><ul id='XmC3S'></ul>
                    <tfoot id='XmC3S'></tfoot>
                  1. <small id='XmC3S'></small><noframes id='XmC3S'>

                      <i id='XmC3S'><tr id='XmC3S'><dt id='XmC3S'><q id='XmC3S'><span id='XmC3S'><b id='XmC3S'><form id='XmC3S'><ins id='XmC3S'></ins><ul id='XmC3S'></ul><sub id='XmC3S'></sub></form><legend id='XmC3S'></legend><bdo id='XmC3S'><pre id='XmC3S'><center id='XmC3S'></center></pre></bdo></b><th id='XmC3S'></th></span></q></dt></tr></i><div id='XmC3S'><tfoot id='XmC3S'></tfoot><dl id='XmC3S'><fieldset id='XmC3S'></fieldset></dl></div>