在 PHP 中运行控制台程序 [英] Running console program in PHP

查看:39
本文介绍了在 PHP 中运行控制台程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的 PHP 代码来执行一个控制台程序:

I wrote a simple PHP code to execute a console program:

<?php
$cmd = escapeshellcmd('progName.exe arg1 arg2 arg3');
exec($cmd);
?>

如果我直接在服务器上的控制台上运行命令,它就可以工作.但是,当我在浏览器上运行 PHP 时,它不起作用.进程 progName.exe 正在运行(使用服务器上的任务管理器检查),但它从未完成.该程序应该从参数中计算一些参数并将结果写入二进制文件,并生成一个 .WAV 文件.这是我在浏览器上收到的错误消息:

If I run the command on the console directly on the server, it works. However, when I run the PHP on the browser, it doesn't work. The process progName.exe is running (checked using Task Manager on the server), but it never finishes. This program is supposed to compute some parameters from the arguments and write the result to a binary file, and also produce a .WAV file. Here is the error message I get on the browser:

Error Summary
HTTP Error 500.0 - Internal Server Error
C:\php\php-cgi.exe - The FastCGI process exceeded configured activity timeout

Detailed Error Information
      Module    FastCgiModule
Notification    ExecuteRequestHandler
     Handler    PHP
  Error Code    0x80070102

然后我写了一个简单的控制台程序,将一个句子写入一个文本文件(writeTxt.exe hello.txt).使用相同的 PHP 脚本,我在浏览器上运行它并且它工作正常.

Then I wrote a simple console program that write a sentence to a text file (writeTxt.exe hello.txt). Using the same PHP script, I ran it on the browser and it works.

我已经尝试增加服务器上的超时时间,但仍然出现相同的错误.什么可能导致此问题?

I already tried to increase the timeout on the server, but still have the same error. What could cause this problem?

推荐答案

当你在 PHP 中使用 exec 函数(例如 exec('dir')),PHP 一直等到它结束你把它发送到后台,PHP 直接返回 (查看文档,尤其是评论).

When you execute a program in PHP using the exec function (e.g. exec('dir')), PHP waits until it is ended or you sent it to the background and PHP comes back directly (see documentation, especially the comments).

根据您发布的 PHP 源代码 ($cmd = escapeshellcmd('progName.exe arg1 arg2 arg3');) 该程序不会被 PHP 发送到后台 - 所以剩下的是 progName.exe...

According to your posted PHP sources ($cmd = escapeshellcmd('progName.exe arg1 arg2 arg3');) the program is not sent to background by PHP - so what stays is that progName.exe...

  1. ...将自身或分支发送到后台(不太可能,但请查看 progName.exe 的来源)
  2. ...正在等待输入(<--这是我最喜欢的)
  3. 我错过了一些东西;-)

正如我所说,我敢打赌这是第二个选择.希望有所帮助.

As I said I bet it is the second option. Hope that helped a bit.

这篇关于在 PHP 中运行控制台程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆