Hot questions for Using Ubuntu in php

Question:

I am making a website which requires a java program to run on the server (it is a ubuntu server). I want to achieve this by executing it from php. I tried 'shell_exec' and 'exec', but they both don't open a terminal window or execute the jar file. It did work on my windows pc, but I want to have it work on my linux server as well. I am using xampp as server.

The command I used that worked on windows:

shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS");

Answer:

I fixed it myself by adding '2>&1 &' at the end of the shell_exec() command. This makes the php script wait untill the program is finished.

The new command:

shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS 2>&1 &");

Question:

I am executing java classes from php.

For testing purposes, I have a php page. When I navigate to that page, it executes a java program that waits 5 seconds and outputs something.

It appears that when i navigate to the page at the same time from different browser windows, the first window takes 5 seconds to output the message, the second one takes 10 seconds, the thrid one 15 seconds and so on.

What I want is for the java processes to run in parallel, so that all windows output the result roughly at the same time, but I have no idea how to do this. I feel like there is an easy solution, but I just cannot find it anywhere on the internet.

I have also tried to use executing it with nohup and & to run in the background, but the same thing happens.


Answer:

It seems you have some locking problem in place that prevents the parallel execution. Are you using the PHP session module? The default session storage module stores the session data in a file that is locked during the execution of a page. If you load the page twice in the same browser, the second request is blocked by the session module until the first request finishes.

Perform the second request in another browser to check if that is your problem. If so, you can call session_write_close() to release the lock if you don't require to store any more data in the session.

Question:

I am a complete beginner in PHP.

I want to execute a java .jar file using PHP.The jar file takes input from STDIN and generates the output at STDOUT.To execute the jar file this is what I do in my Ubuntu Linux terminal:

./java/bin/java -jar abc.jar
<here goes the STDIN>
Ctrl+D
<The output gets generated here at the STDOUT>

This works perfect.However when I replicate this in PHP as:

$p=shell_exec("./java/bin/java -jar abc.jar $s 0<&-");

This sends $p the output generated when STDIN is empty.Which definitely means that $s is not being passed.I am perfectly sure my jar file works perfectly,it's the php script where my mistake is.Can anyone please point out my mistake? Thanks in advance.


Answer:

Complete shot in the dark, but:

$p=shell_exec(sprintf('./java/bin/java -jar abc.jar "%s" 0<&-', addslashes($s)));

It could be a case that $s contains unescaped characters that ruin the command syntax.