• <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)

        PHP exec() return value for background process (linux)(PHP exec() 后台进程的返回值 (linux))

            <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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                • <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>
                              <tbody id='XmC3S'></tbody>