简体   繁体   English

如何通过PHP代码运行Shell命令?

[英]How to run a shell command through PHP code?

I am trying to run a Jar file in the backend of my php code.But I am not getting the desired output to it.There is a jar file which runs in the background and returns the Page Rank of any of the keyword and Domain given to it. 我正在尝试在我的php代码的后端运行一个Jar文件。但是我没有得到想要的输出。有一个jar文件在后台运行并返回给定的任何关键字和Domain的Page Rank对此。 I am attaching the code,please suggest me any solution to it,because when I run it on the terminal,it is giving correct output. 我正在附加代码,请为我提出任何解决方案,因为当我在终端上运行它时,它会提供正确的输出。

Here is the Code : 这是代码:

    <?php
set_time_limit(0);
function returnJarPath()
{
    $jarPath = $_SERVER['DOCUMENT_ROOT'] . "myFolder/tools_new/includes/Rank.jar";
    return $jarPath;
}
$jar = returnJarPath();
$command = "java -jar $jar aspdotnet/microsoft.com";//Passing the Argument to the Jar file.


$shellOutput = shell_exec($command);
    print "The Shell Output is : " ; var_dump($shellOutput);print "<br />";
exec($command,$executeCommmand);
    print "The Exec returns the value : " ; var_dump($executeCommmand);print "<br />";
passthru($command,$passthruCommand);
    print "The Passthru returns the value : " . $passthruCommand. "<br />";
?>

I just checked apache's error log and the last error I found was : 我刚刚检查了apache的错误日志,发现的最后一个错误是:

sh: java: command not found sh:java:找不到命令

But as I have already said,I have been using the same command through SSH to run the Java command.So there's no such possibility of not having JAVA installed on the server.Please help me out of this mess... 但是正如我已经说过的那样,我一直在通过SSH使用相同的命令来运行Java命令。因此,没有在服务器上未安装JAVA的可能性。请帮我摆脱这个混乱...

If the jar file writes to standard output you can use exec. 如果jar文件写入标准输出,则可以使用exec。

Here is an example how I use it: 这是我如何使用它的示例:

may be first: exec("cd jar dir"); // if jar fine needs to be executed from the same dir
$output = exec("/usr/bin/java -jar $jar aspdotnet/microsoft.com");

But as you say: 但是正如你所说:

sh: java: command not found

It means the there is no path alias to java from php. 这意味着没有从PHP到Java的路径别名。 Just use the full java path to the executable /usr/bin/java. 只需使用可执行文件/ usr / bin / java的完整Java路径即可。

Given you are calling java . 鉴于您正在调用java My bet is the output is being displayed in the Java Console, and not in the shell, where PHP could pull the text information. 我敢打赌,输出将显示在Java控制台中,而不是显示在PHP可提取文本信息的外壳中。

How to solve this dilemma? 如何解决这个难题?

Well you could write the results to a file, if you have the java source to modify, and then read that file through php to get the results. 好吧,如果您要修改Java源代码,则可以将结果写入文件,然后通过php读取该文件以获取结果。 The possibility of a collision here would be pretty good. 这里发生碰撞的可能性非常好。 The other option is to have Java connect to your MySQL database (if you had one) and then run the java then query the database for the response. 另一个选择是让Java连接到MySQL数据库(如果有的话),然后运行Java,然后在数据库中查询响应。 Of course, you would need to pass Java a way for it to input the data to insert a record you could identify (a hash of some sort), I have never done that in Java, just a theory of how you might be able to do it . 当然,您需要为Java传递一种输入数据的方式,以插入您可以识别的记录(某种哈希),我从未在Java中做到过,只是关于您如何能够做吧

Update 更新资料

You may want to try the standard output as suggested by darko petreski as another option as well. 您可能还想尝试Darko Petreski建议的标准输出,作为另一种选择。

If the PHP code is to be executed in a server (and not via command line) the user that runs the java executable is www-data, not you. 如果要在服务器中(而不是通过命令行)执行PHP代码,则运行Java可执行文件的用户是www-data,而不是您。 In that case make sure that www-data has the permissions to read the jar file and to execute the java executable 在这种情况下,请确保www-data有权读取jar文件和执行Java可执行文件。

The first thing that I would check/change is the line in the function where you are building the $jarPath variable from this: 我要检查/更改的第一件事是$jarPath构建$jarPath变量的函数行:

$jarPath = $_SERVER['DOCUMENT_ROOT'] . "myFolder/tools_new/includes/Rank.jar";

to this: 对此:

$jarPath = $_SERVER['DOCUMENT_ROOT'] . "/myFolder/tools_new/includes/Rank.jar";

The trailing slash may not be present in $_SERVER['DOCUMENT_ROOT'] which could cause issues. $_SERVER['DOCUMENT_ROOT']可能没有尾部斜杠,这可能会导致问题。

I am assuming that when you say it runs from the console, you are running the java command like so: 我假设当您说它从控制台运行时,您正在像这样运行java命令:

$ java -jar /rest/of/path/myFolder/tools_new/includes/Rank.jar aspdotnet/microsoft.com

I would ensure that you include the path to the java binary in the $command variable like so... 我会确保像这样...在$command变量中包含Java二进制文件的路径。

$command = "/path/to/java -jar $jar aspdotnet/microsoft.com";

The user that owns the web server process may not have a $PATH variable that includes the path to the Java binary. 拥有Web服务器进程的用户可能没有$ PATH变量,该变量包含Java二进制文件的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM