php-symfonyHow to set up a cron job in PHP Symfony?
Setting up a cron job in PHP Symfony is easy.
First, create a command class in the src/Command
directory.
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCronJobCommand extends Command
{
protected static $defaultName = 'app:my-cron-job';
protected function configure()
{
$this
->setDescription('My cron job description.')
->setHelp('This command allows you to create a cron job...');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Do something...
}
}
Then, add the command to the config/services.yaml
file:
services:
App\Command\MyCronJobCommand:
tags:
- { name: console.command }
Finally, add the cron job to the crontab file:
* * * * * cd /path-to-your-project && php bin/console app:my-cron-job
This will execute the command every minute.
Helpful links
More of Php Symfony
- How to manage sessions in Symfony with PHP?
- How to do a health check in PHP Symfony?
- How to create a model in PHP Symfony?
- How to integrate Vue.js with PHP Symfony?
- How to install PHP Symfony on Ubuntu?
- How to do testing with PHP Symfony?
- How to use the messenger component in PHP Symfony?
- How to create a backend with PHP Symfony?
- How to use the PHP Symfony findOneBy method?
- How to use the PHP Symfony findBy method?
See more codes...