I've built a command in Symfony 4 which works fine when run from CLI but doesn't execute when run by cron. The console command is being run and no errors are thrown.
I've even thrown one in execute and it does not fail/error out:
public function execute(InputInterface $input, OutputInterface $output): void
{
throw new \Exception('I am doing something');
}
My full command looks like this and is autowired:
<?php declare(strict_types = 1);
namespace CRMInterface\Command\Customer;
use CRMInterface\Service\Customer\CustomerSyncService;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CustomerSyncCommand extends Command
{
const COMMAND_NAME = 'crm:sync:customer';
/**
* @var CustomerSyncService
*/
private $syncService;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param CustomerSyncService $syncService
* @param LoggerInterface $logger
*/
public function __construct(CustomerSyncService $syncService, LoggerInterface $logger)
{
parent::__construct(self::COMMAND_NAME);
$this->syncService = $syncService;
$this->logger = $logger;
}
protected function configure()
{
$this
->setName(self::COMMAND_NAME)
->setDescription('Processes outstanding portal sync tasks');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
public function execute(InputInterface $input, OutputInterface $output): void
{
$this->logger->info('Syncing customers...');
try {
$this->syncService->sync();
$this->logger->info('Customer sync complete.');
} catch (\Exception $e) {
$this->logger->error('Customer sync failed: ' . $e->getMessage());
}
}
}
My cron job is as follows:
*/3 * * * * www-data cd /var/www/html && /usr/local/bin/php bin/console crm:sync:customer --env=prod
This set up works in a Symfony 3 app and a Symfony 2.8 app I have running but not with 4 and it's driving me batty.
My bin/console is as follows - I've taken out the stuff to do with APP_ENV because it was superfluous in my case and was failing due to the lack of env vars in cron.
#!/usr/bin/env php
<?php
use CRMInterface\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
set_time_limit(0);
require __DIR__.'/../vendor/autoload.php';
if (!class_exists(Application::class)) {
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
}
$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], $_ENV['APP_ENV'] ?? 'dev');
$debug = ($_ENV['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption(['--no-debug', '']);
if ($debug) {
umask(0000);
if (class_exists(Debug::class)) {
Debug::enable();
}
}
$kernel = new Kernel($env, $debug);
$application = new Application($kernel);
$application->run($input);
Can anyone point me in the right direction as to why the command is running but not getting down to execute?
It's almost as if it's just running bin/console without the command... could it be something to do with lazy loading?
bin/console crm:sync:customer --env=prodmanually?grep syslogfor cron errors, but I see that in the line of crontab you have(...) www-data cd && (...)I believe it's failing there because of undefined command*/3 * * * * /usr/local/bin/php /var/www/html/bin/console crm:sync:customer --env=prodbin/consolefile ok as it's coming up in the monitoring. When I add a statement in that file to throw an exception if the command name iscrm:sync:customerthen that exception is thrown...