使用 Ant 在后台运行 PHP 脚本 [英] Running PHP script in background using Ant

查看:33
本文介绍了使用 Ant 在后台运行 PHP 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我现在的雇主,我们使用 Ant 来执行我们的构建脚本,我需要在我们的 build.xml 文件中添加一个目标,该目标将在后台启动 4 个作为 Gearman 工人的 PHP 脚本,然后停止这些脚本一次构建完成.

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.

我已经查看了parallel"和daemons"指令(这是正确的词吗?)但我对 Ant 的经验不足,无法追踪我缺少的有关如何确保脚本运行的信息在后台.

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.

推荐答案

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

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

这篇关于使用 Ant 在后台运行 PHP 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆