Symfony命令由cron运行,但没有效果。在命令行上运行正常

时间:2017-07-19 08:14:23

标签: php symfony cron

我目前正在使用Symfony 2.7制作邮件客户端。我已经命令用IMAP收集新邮件并从中创建实体。 此命令在命令行中运行正常,它收集并显示新邮件。

这是命令:

protected function configure()
{
    parent::configure();
    $this
        ->setName('app:mails:collect')
        ->setDescription('Collects new mails from every Mailboxes.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $em = $this->getContainer()->get('doctrine')->getEntityManager();

    $mailboxes = $em->getRepository('MIPMailBundle:MailBox')->findAllActive();

    foreach ($mailboxes as $mailbox){
        $imapBox = new ImapMailbox('{'.$mailbox->getServer().':143/notls/norsh/novalidate-cert}INBOX',
                                    $mailbox->getAdress(), $mailbox->getPassword(), "web/mails/", "UTF-8");

        if (count($mailbox->getMails()) == 0){
            $output->writeln("Getting mails from mailbox...");
            $mailsIds = $imapBox->searchMailbox('ALL');
            if(!$mailsIds) {
                $output->writeln($mailbox->getAdress() . " is empty");
            }
        } else {
            $output->writeln("Searching for new mails...");
            $mailsIds = $imapBox->searchMailbox('UNSEEN');
            if(!$mailsIds) {
                $output->writeln("No new mail for " . $mailbox);
            }
        }

        foreach ($mailsIds as $mailId){

            //Creates new mail entities...

            $imapBox->markMailAsRead($mailId);
        }
    }

    $em->flush();
}

我希望服务器每分钟运行一次这个命令。所以我为此做了一个cronjob:

* * * * * /path/to/myapp/app/console app:mails:collect

但是如果被cron运行,命令就不起作用了。我试图将输出写入文件,但文件为空。在cron日志中,我可以看到执行的工作:

Jul 19 09:39:01 dev CRON[26967]: (web) CMD (/path/to/myapp/app/console app:mails:collect)

但它不起作用......我试图通过在命令中指定环境来指定环境(我在dev env中工作):

* * * * * /path/to/myapp/app/console app:mails:collect --env=dev

这是不成功的。知道为什么吗?谢谢

2 个答案:

答案 0 :(得分:3)

这对我来说很好。首先cd进入目录然后运行。这样做会发生什么。

* * * * * cd /path/to/myapp && php app/console app:mails:collect --env=dev

使用bin/consoleapp/console!取决于您的设置。

注意:当你的选项用完时,尝试使用一个非常简单的命令(比如打印" hello world"到终端)来查看它是你的命令还是别的什么。

答案 1 :(得分:1)

尝试指定php可执行路径:

* * * * * /path/to/php /path/to/myapp/app/console app:mails:collect --env=dev

例如:

* * * * * /usr/bin/php /path/to/myapp/app/console app:mails:collect --env=dev
相关问题