Blackwaxx
Blackwaxx

Reputation: 45

Symfony command runned by cron, but no effect. Runs fine on command line

I'm currently making a mail client with Symfony 2.7. I've made a command to collect new mails with IMAP and create entities from them. This command runs fine in command line, it collects and displays new mails.

Here is the command :

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();
}

I want this command to be runned every minute by the server. So I've made a cronjob for this :

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

But the command doesn't work if runned by cron. I tried to write the output to a file, but the file is empty. In cron logs, I can see the job executed :

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

but it doesn't work... I tried to specify environment (i work in dev env) by specifying it in the command :

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

This was unsuccesful. Any idea why ? Thanks

Upvotes: 1

Views: 1742

Answers (2)

BentCoder
BentCoder

Reputation: 12740

This works fine for me. cd into directory first then run. Do this see what happens.

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

Use either bin/console or app/console! Depending on your setup.

Note: When you run out of options, try to use a very simple command (something like printing "hello world" to terminal) to see if it is your command or something else.

Upvotes: 4

Bogdan Mantescu
Bogdan Mantescu

Reputation: 121

try to specify php executable path:

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

ex:

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

Upvotes: 1

Related Questions