Home » Php » Running PHP script in background using Ant

Running PHP script in background using Ant

Posted by: admin July 12, 2020 Leave a comment


At my current employer we use Ant to execute our build scripts, and I need to add a target to our build.xml file that will start up 4 PHP scripts that are Gearman workers in the background, and then stop those scripts once the build is done.

I’ve looked at the ‘parallel’ and ‘daemons’ directives (is that the right word?) but I am not experienced enough with Ant to track down the info I’m missing on how to make sure the script runs in the background.

How to&Answers:

As you’re not getting many answers I’ll suggest a low tech method that might get you start…

Use an ant exec task to fire off 4 background php processes writing their pid to a file which includes the build number (from environment presumably) to identify it.

Once build is complete run script again with a stop parameter and use the file naming system to find process ids, kill take and delete piddling files. Probably worth you having some sort of stale job cleaner in there too.

Shouldn’t be too hard to knock up something that works until you can find a more elegant solution.


Is this any good for you:

test.php: (this would be your worker script)

<?php while (true) { echo "Hello world" . PHP_EOL; sleep(5); }




if [ -z $TEST_RUNNERS ]; then

if [ -z $BUILD_NUMBER ]; then
  echo "Can not run without a build number";
  exit 1;


if [ -e $FILE ]; then
    while read line;
        echo "Killing process " $line
        kill -9 $line
    echo "Deleting PID file"
    rm -f $FILE
    exit 0
fi  < $FILE

for ((i=1; i<=$TEST_RUNNERS; i++)); do
  echo "Setting up test runner number " $i " of " $TEST_RUNNERS;
  php $FILE_TO_RUN &
  echo "PID number: " $!
  echo $! >> "${BUILD_NUMBER}.run"
exit 0