php system() shell_exec() 挂起浏览器

发布于 2024-11-13 07:35:27 字数 646 浏览 2 评论 0原文

可能的重复:
PHP 中的异步 shell 执行

我需要在后台运行一个 java 程序。

process.php 包含

shell_exec("php php_cli.php")

php_cli.php 包含

shell_exec("java -jar BiForce.jar settings.ini > log.txt");

我正在使用 ajax 异步调用 process.php

当我单击网页中调用 ajax 函数的链接时(用于运行 process.php) php) 网页显示“正在加载”。当我同时单击其他链接时,它没有响应。

java程序大约需要24小时才能执行完毕,因此用户不会等到执行结束。

问题在于,单击链接时浏览器会继续加载并且不会转到其他页面。

我也尝试过使用 system(),但同样的问题......

我们将不胜感激。

Possible Duplicate:
Asynchronous shell exec in PHP

i need to run a java program in the background.

process.php contains

shell_exec("php php_cli.php")

php_cli.php contains

shell_exec("java -jar BiForce.jar settings.ini > log.txt");

I am calling process.php asynchronously using ajax

When i click the link in the webpage that calls ajax function (for running process.php) the webage shows "loading". when i click other links at the same time it does not responds.

The java program takes about 24 hours to finish executing, so user will not wait until the execution ends.

The problem is that the browser keeps on loading and does not go to other pages when clicked the link.

I also tried with system(), but the same problem ....

Help will greatly be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我入戏太深 2024-11-20 07:35:27

使用 shell_exec 等待命令挂起,这就是您的脚本正在做的事情。

如果您的命令没有任何等待时间,那么您的脚本也不会。

您可以从原始脚本调用另一个 PHP 脚本,而无需等待它挂起:

$processId = shell_exec(
    "nohup " .                          // Runs a command, ignoring hangup signals.
    "nice " .                           // "Adjusted niceness" :) Read nice --help
    "/usr/bin/php -c " .                // Path to your PHP executable.
    "/path/to/php.ini -f " .            // Path to your PHP config.
    "/var/www/php_cli.php " .           // Path to the script you want to execute.
    "action=generate > /process.log " . // Log file.
    "& echo $!"                         // Make sure it returns only the process id.
    );

然后可以使用以下命令检测脚本是否完成:

exec('ps ' . $processId, $processState);
// exec returns the result of the command - but we need to store the process state.
// The third param is a referenced variable.

// First key in $processState is that it's running.
// Second key would be that it has exited.
if (count($processState) < 2) {
    // Process has ended.
}

Using shell_exec waits for the command to hang up, so that's what your script is doing.

If your command doesn't have any wait time, then your script will not either.

You can call another PHP script from your original, without waiting for it to hang up:

$processId = shell_exec(
    "nohup " .                          // Runs a command, ignoring hangup signals.
    "nice " .                           // "Adjusted niceness" :) Read nice --help
    "/usr/bin/php -c " .                // Path to your PHP executable.
    "/path/to/php.ini -f " .            // Path to your PHP config.
    "/var/www/php_cli.php " .           // Path to the script you want to execute.
    "action=generate > /process.log " . // Log file.
    "& echo $!"                         // Make sure it returns only the process id.
    );

It is then possible to detect whether or not the script is finished by using this command:

exec('ps ' . $processId, $processState);
// exec returns the result of the command - but we need to store the process state.
// The third param is a referenced variable.

// First key in $processState is that it's running.
// Second key would be that it has exited.
if (count($processState) < 2) {
    // Process has ended.
}
落花浅忆 2024-11-20 07:35:27

您可以在显示的页面中调用该命令,但在末尾附加 &

shell_exec("java -jar BiForce.jar settings.ini > log.txt &");

这样该进程就会在后台启动。

另外,不需要(除非由您的应用程序定义)创建一个 process.php ,它本身通过 shell exec 调用 php。您可以通过包含到另一个文件来归档相同的功能。

You could call the command in the page displayed, but appending an & at the end:

shell_exec("java -jar BiForce.jar settings.ini > log.txt &");

This way the process is launched on the background.

Also, there is no need (unless defined by your application) to create a process.php wich itself calls php via a shell exec. You could archive the same functionality via an include to the other file.

蒗幽 2024-11-20 07:35:27

与普通 shell 脚本一样,您可以使用 & 符号来后台处理进程:

shell_exec("java -jar BiForce.jar settings.ini > log.txt &");

请参阅 PHP 中的异步 shell 执行< /a> .

As in normal shell scripting you can use the ampersand to background the process:

shell_exec("java -jar BiForce.jar settings.ini > log.txt &");

See Asynchronous shell exec in PHP .

静谧 2024-11-20 07:35:27

首先,您可能想要重新设计这个概念。我不确定这些程序到底做了什么,但显然这可能会导致潜在的问题...

这就是我建议你做的,而不是通过 PHP 启动外部进程:

  • 你的 ajax 调用在某些地方创建(或重用)一个文件临时目录(可能使用用户会话来生成该文件)
    • 一些数据写入文件,请求结束
  • 你的jar被单独启动,并无限期运行
    • Java 程序会定期扫描临时目录中是否有新文件,或者某些文件是否已被修改
    • 解析它,并执行长达 24 小时的流程,或在必要时调整任何先前的执行

。甚至可以使用套接字或任何其他方式与该 Java 程序进行通信。

让Java程序一直运行而不是启动一个新进程的优点是能够在应用程序的生命周期内重用系统资源;例如,如果您的程序正在使用数据库连接,或任何数据、缓存等。

First, you might want to redesign this concept. I am not sure exactly what these programs do, but clearly this is can lead to potential problems...

This is what I suggest you do, instead of starting external processes via PHP:

  • Your ajax call creates (or reuse) a file in some temporary directory (probably using the user session to generate that file)
    • some data is written unto the file, and the request ends
  • Your jar is launched separately, and runs indefinitely
    • At regular intervals, the Java program scans the temporary directory for new files, or if some file has been modified
    • parse it, and execute the 24 hour long process, or adjust any previous execution if necessary

Along the same idea, you can even use sockets instead to communicate with that Java program, or any other way.

The advantage of having the Java program running all the time instead of starting a new process is to be able to reuse system resources within the lifetime of the application; for example, if your program is using DB connections, or any data, cache, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文