In order to automate tasks that should run on a regular basis, you will need a cronjob. Fortunately, creating cronjobs with symfony is a piece of cake.

First, you will need to create a batch task. Navigate to your project directory and enter

symfony generate:task [your task] 

This will generate a task skeleton for you in lib/task/[your task]Task.class.php , which contains two important methods: configure() and execute(). The names are pretty self-explanatory. All your code should go in the execute() method. You might want to do some configurations first. Change the following lines in configure() accordingly:

$this->namespace        = 'project';
$this->name             = '[name-for-your-task]';
$this->briefDescription = '[some short explanation of what your task does]';

Before you start hacking your code into the execute() method, I would recommend to test-run it in a normal symfony module first. When you’re done, copy&paste the action of your test module into the execute() method. Be aware that tasks don’t contain a view and, therefore, you will need to echo any output directly in the method itself. An example that, frankly speaking, does nothing useful at all:

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 "I just did nothing at all, but at least I did it successfully!\n\n";
}

Let’s give it a test run. Open your command line and enter

symfony project:[your task]

If you see the expected output, you’re ready to go to the next step.

Please notice: The paths I’ve used apply to OpenSuse 11.1. If you run another Linux distribution, you might have to amend them.

Open your servers crontab at /etc/crontab in your favourite text editor and add the following line 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

Update: Thanks to Marcell Fülöp for the suggestion to use >> to avoid overriding existing log entries

Explaining every detail of cronjobs would definitely blow the extent of this how-to and there are some very good sites out there that can help you understand. Just a quick explanation: This task runs every 5 minutes, changes to your symfony application directory, executes your task (please note that you don’t have access to your $PATH variables, so no symfony shortcut!) and outputs the result to /log/crontab.log .

  4 Responses to “symfony and Cronjobs”

  1. nice tutorial!

  2. schreib mal wieder was sinnvolles ^^

  3. Hi how to set crontab to write log without overwritting the current log datas

    • Simply use >> instead of the > in your crontab line like:

      */5 * * * * cd [YOUR SF APP DIR] && /usr/bin/symfony project:[YOUR TASK] >> [YOUR SF APP DIR]/log/crontab.log

      This will append to the existing file. Make sure you end your PHP script’s output with a “\n” so each execution’s log will start in a new line.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">

   
© 2011 burgiblog Suffusion theme by Sayontan Sinha