user2628641
user2628641

Reputation: 2154

PHP: Why shell cmd runs fine in shell but not in shell_exec()/exec() executed on a browser?

I tried to execute a jar file using php's shell_exec(). The pgp file runs on a server.

I researched the problem and tried the following things but no success so far:

  1. I specify the full java path in the command
  2. I make sure shell_exec()/exec() function is not disabled on the server, the server is not run on safe mode
  3. I change the permission of the jar file to be executable
  4. I also try using exec()
  5. I test command like exec("ls", $out), and it works fine

The command looks like:

shell_exec("/usr/lib/jvm/java-7-oracle/bin/java -jar PATH_TO_MY_JAR_FILE.jar");

I copy and paste the command into shell and it runs fine.

----------------------------Update-------------------------------------

I made some progress on debugging and want to update the problem a little bit.

I tried to debug using

exec("command from above", $output, $exit_code)
echo $output;
echo $exit_code;

and done some fixing and now I am sure the php script is executed but it's not doing what I want.

So the jar file's purpose is to create a json file in the system. I specify the absolute path for this json.

Now, after I run the php file, the $output shows

Array ( [0] => {"balance":"c","num":"b","is_vip":true,"name":"a"} ) 

This is the same output when I run the command in shell

$exit_code's value is 10

But I can't find the json file in the system.

Upvotes: 1

Views: 891

Answers (1)

sics
sics

Reputation: 1318

Did you check if the php safe mode is enabled on the server? The documentation of http://php.net/manual/en/function.shell-exec.php says:

Note: This function is disabled when PHP is running in safe mode.

Nevertheless: the documentation also says, that the safe_mode is deprecated since PHP5.3 so hopefully, this does not affect your server.

Additionally you should check if the php user is allowed to execute +x the java binary.

Upvotes: 1

Related Questions