且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Ant 在后台运行 PHP 脚本

更新时间:2023-09-15 16:25:10

由于你没有得到很多答案,我会建议一个低技术的方法,可能会让你开始......

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

使用 ant exec 任务触发 4 个后台 php 进程,将它们的 pid 写入一个文件,该文件包含构建号(可能来自环境)以识别它.

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.

一旦构建完成,再次运行带有 stop 参数的脚本,并使用文件命名系统来查找进程 ID、终止并删除 piddling 文件.可能值得你在那里也有一些陈旧的工作清洁工.

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.

这对你有好处吗:

test.php:(这将是您的工作脚本)

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

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

runner.sh:

#!/usr/bin/bash

FILE_TO_RUN=test.php

if [ -z $TEST_RUNNERS ]; then
  TEST_RUNNERS=4;
fi;

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

FILE="${BUILD_NUMBER}.run"

if [ -e $FILE ]; then
    while read line;
    do
        echo "Killing process " $line
        kill -9 $line
    done
    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"
done
exit 0