How to configure cron jobs in Symfony

Categories : Article, Symfony

Many a times, while you are working on a Symfony web development project, you might come across the need to create a scheduled task that runs in the background at a specific time on your web server. This is exactly what a cron job can help you with. Cron jobs are created with the help of a program called crontab on a UNIX/LINUX type web server. Cron jobs very easily automate tasks that you need to run on a regular basis.

Here are the steps to create a cron job:


Navigate to your project directory and enter the following:

symfony generate:task [example task

This generates a skeleton task on your web server under lib/task/[ example task]Task.class.php.
This essentially contains two important aspects or methods: configure() and extecute().
All the code for the cron job goes in the execute()


There are some configurations that you might want to do before you place all your executable code in the execute(). The following changes will be required in the configure().
$this->namespace = ‘project’;
$this->name = ‘[name-for-example-task]’;
$this->briefDescription = ‘[some short explanation of what example task does]’;


It’s recommended that you run the code for execution as a Symfony module before setting it up for a cron job only then copy paste it to the execute() method. Now, remember that tasks do not have a visible output, so you will need to echo it in the method. For example:
protected function execute($arguments = array(), $options = array())
// initialize the database connection
$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options[‘connection’] ? $options[‘connection’] : null)->getConnection();

// add your code here

echo “the output here!\n\n”;

This completes the cron job configuration and execution requirements now, let’s see how to test it.


Open the commandline and enter the following
symfony project:[example task]
Check whether you see the expected output.

After that’s done, you can open crontab by going to /etc/crontab. This can be opened in a simple text editor. All you need to do is, add this to the end of the document.

*/5 * * * * cd [YOUR SF APP DIR] && /usr/bin/symfony project:[YOUR TASK] >>[YOUR SF APP DIR]/log/crontab.log
This was a very basic explanation. To check the results you can view the log file for crontab at /log/crontab.log .

[Total: 2    Average: 5/5]

Client Saying

Chad Gundry, the CEO of says that he has worked extensively with Brainvire team since last 6 to 7...
Mr. Chad Gundry
Just wanted to say massive thanks for all the help from the Brainvire team. After years of having a website...
Mr. Rohit Singla
We have dealt with a lot of web development firms and other tech support companies, none come close to Brainvire....
Mr. Matthew
Daniel Coburn, the president of gives the honest opinion regarding his experience working with Brainvire for developing his social...
Mr. Daniel Coburn
When the world was moving at a very speedy pace, Brainvire helped us stay connected. Thanks to their unique services...
Travis French

Latest Happenings

  • 04 Oct 2016
    Brainvire Making great Strides with Symfony Development Services [ + ]
  • 29 Sep 2016
    Brainvire augments its List of Offerings with Magento Sears Integration Services [ + ]