How to Use Symfony and Cron

By marc • Apr 30th, 2008 • Category: Symfony

At some point you may need to have your symfony do some sort of process through the command line and trigger it through a cron job. I’ll will walk you through the steps in order to get it working properly.

The Secret of the Batch

Luckily, symfony is already set up to make it easy for you to create small controllers that will be run from the command line. The “/batch” directory is where you will be putting your special controller file, instead of the “/web” directory.

This batch file is looks pretty much like the front controllers that go in the “web” directory. The only thing you will be leaving out is the dispatch method which starts running the controller and action. We are going to be replacing it with a line that still fires up the sfContext instance so that we have access to our database, etc.

Example Batch File:

  1.  
  2. <?php
  3.  
  4. // this file is saved as SF_PROJECT/batch/cron_example.php
  5.  
  6. define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/..'));
  7. define('SF_APP', 'frontend');
  8. define('SF_ENVIRONMENT', 'prod');
  9. define('SF_DEBUG', true);
  10.  
  11. require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
  12.  
  13. // get the application instance
  14. sfContext::getInstance();
  15.  
  16. // put whatever code you need here…
  17. echo "\r\n running cron job….\r\n";

As you can see it’s pretty similar to a standard front controller file. Now all you need to do is add the code for what your cron job will actually need to do.

Now lets make sure that the job actually runs by manually calling it from the command line (below I am assuming that you are in the root directory of your project.):

  1.  
  2. $ php batch/cron_example.php
  3.  
  4. running cron job…

If you get the excepted output, then everything is good to go. Now we need to move on to the final step of setting up or crontab file

The Crontab File

We just need to edit our crontab file so it runs the batch file.

  1.  
  2. # run our batch process every minute and force output to null
  3. * * * * * * php /home/me/my_project/batch/cron_example.php > /dev/null 2>&1

As you can see, it’s pretty darn simple to get started, and can surely be expanded upon.

Check out my previous article about custom logging if you want to log to a file from your cron job.

marc is a Web Developer in Los Angeles, CA. He likes to receive comments :)
All posts by marc

4 Responses »

  1. Woohoo!

    I always had that problem while trying to connect tru propel on the cron job.

    very nice post

  2. Hi Marc,

    Can you please put light on how to set layout(false) in cron file.
    For me everyhting is working fine, except on command line it also displays html.

    Thanks in advance!

  3. @Aniruddha: I’m not sure why it would be displaying any html output. Since my example just uses the sfContext::getInstance() it should just load up the core framework, but shouldn’t run any of the view classes.

    The only thing that I can think is that it is outputting some debug trace stack or something similar.

    What is the html that is getting displayed? Let me know and maybe I can figure out why that’s happening in your case :)

  4. Hello Marc,

    Thanks for the reply & sorry for delay from my side.

    I have fixed the issue. The issue was I have not used “symfony init-batch ….”
    Instead I have directly created a php file in batch folder.

    Thank you very much for your quick response.

Leave a Reply