Neha
Neha

Reputation: 75

execute java program in the background while loading a page using php

I am trying to run a java program in the background using php. I am able to the run the program but the page doesn't load till java program completes executing.

I am able to run shell scripts in the background using php. But if i give the same command for java, it doesn't run in the background.

$command = "java hello";
$outputfile = "out1";
$pidfile = "out2";
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $command, $outputfile, $pidfile));

How can i run the program in the background?

Upvotes: 1

Views: 830

Answers (2)

Ryan
Ryan

Reputation: 21

I got this to work with the following:

shell_exec('java app.jar > /dev/null &');
exit();

Specifically I was trying to kick off a couple separate processes through a script in a TeamCity build step.

Upvotes: 2

Logan Bailey
Logan Bailey

Reputation: 7137

In the php docs for exec it says:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

so you need to change the output of the javafile to another terminal, not php or to a file maybe to /dev/null

Upvotes: 0

Related Questions