【发布时间】:2017-12-24 09:04:31
【问题描述】:
我目前正在使用 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)
但它不起作用...我试图通过在命令中指定环境来指定环境(我在开发环境中工作):
* * * * * /path/to/myapp/app/console app:mails:collect --env=dev
这不成功。知道为什么吗?谢谢
【问题讨论】:
-
控制台的路径是相对的还是绝对的?
-
告诉我你的 cron 文件中的确切命令
-
我的 crontab 中的确切命令是:
* * * * * /var/www/MIP/app/console mip:mails:collect --env=dev所以它是绝对路径,是的 -
你必须使用 php 运行它
-
* * * * * php /var/www/MIP/app/console mip:mails:collect --env=dev