【问题标题】:What is the best way to detect console script is runing?检测控制台脚本正在运行的最佳方法是什么?
【发布时间】:2014-12-08 09:54:05
【问题描述】:

我尝试在 Symfony2 中创建控制台命令:

class UpdateCommentsCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('comments:update')
            ->setDescription('Update comments');
            ->addArgument('force', InputArgument::OPTIONAL, 'force', false)
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {

        if (!$input->getArgument('force') && if $flag_this_script_is_active) {
            return;
        }
        $flag_this_script_is_active = True

        $em = $this->getContainer()->get('doctrine')->getEntityManager();
        // Bad comments
        $update_commang = $em->createQuery("
            UPDATE MyBundle:CommentsAll AS c
            SET
                c.is_automoderated =    1
            WHERE
                bla_bla_bla LIMIT 100
        ");
        $result = $update_commang->getResult();
        if ($result > 0) {
                //Re-run this script with --force argument
        } else {
                $flag_this_script_is_active = false
        }

    }
}

这个脚本将由 crone 每 60 秒运行一次,直到所有 commnets 都更改为 is_automoderated = 1

如果上一个脚本没有结束,下一个脚本会看到并且没有运行。

现在我想知道如何设置标志$flag_this_script_is_active。我想创建一些文件,并在脚本结束后将其删除。但也许有更好的方法。也许一些 symfony 函数?

【问题讨论】:

    标签: php symfony cron command


    【解决方案1】:

    您可以为此目的使用 bash 脚本。使用ps aux | grep 'comments:update' 查找已经运行的命令,如果没有找到则运行它。我们在之前的项目中使用了这种方法来检测守护进程是否正在执行。

    【讨论】:

    • 感谢您的回复。我想使用跨平台解决方案。最好是框架原生的。
    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 2011-01-21
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 2019-09-24
    相关资源
    最近更新 更多