Shells and Tasks

Published on and tagged with cakephp  shell  task

Last week I wrote about the new command line features of CakePHP 1.2. And this week it is already outdated what I wrote then ;-) The command line scripts are now called “Shells” instead of “CakeScripts”, so I adapted the previous article to those changes.

With the recent changes a new concept has been introduced: Tasks. Well, tasks are not entirely new as tasks were already used by bake2 (which no longer exists). But now tasks are for shells what components are for controllers.

Tasks are placed in app/vendors/shells/tasks if they are application-specific resp. in vendors/shells/tasks if they are general-purpose tasks. Like shell scripts, task classes have to extend the “Shell” class, but the class name has to end with “Task”. Here a simple example of a task:

// vendors/shells/tasks/test.php
class TestTask extends Shell {

    function execute() {
        // do something

In the core tasks (and in this test task) the main function is called “execute”, but you are free to use any name you want.

The test task can now be used in the shell script in the following way:

// vendors/shells/demo.php
class DemoShell extends Shell {
    var $tasks = array('Test');
    function initialize() {
        // empty
    function main() {

Happy baking :)

18 comments baked

  • Joel Moss

    I noticed all these changes, but am a little unsure as to why there are still tasks when it seems that shells do the same thing. I am rewriting my migrations and fixtures tasks to work with the new console/shell system, and was simply going to add them as shell scripts in /vendors/shells.

    Do you why there are still tasks when shell scripts can do the same thing?

  • kabturek

    >Joel Moss
    and the notes task ! ;)

    thx for the heads up dho
    i cant keep up with the changes myself ;)

  • Nate

    Shells are designed to manage full CLI-based applications, whereas the purpose of Tasks are to break out shell logic into discrete, reusable commands.

  • Joel Moss

    @kabturek: on its way, although since I switched to Mac and textmate, I no longer have a need for the notes task.

    @Nate: hhmmm, seems like a bit of unneeded duplication to me.

  • Nate

    Joel: I guess if you think components are unnecessary duplication of controllers, then yeah.

  • Joel Moss

    @nate: valid point ;)

  • gwoo

    Bake is a good example of why tasks are useful.
    $ cake bake

    the above command will run through bake in interactive mode, if it does not find the database.php it will run the DbConfigTask.

    $ cake bake db_config
    will just run the DbConfigTask

    For something like Acl there are no tasks because everything can be done through methods in the Shell.
    $ cake acl create aro null John
    will run the “create” method in the AclShell

    Complete docs coming soon.

  • cakebaker

    @all: Thanks for your comments :)

  • Joel Moss

    I suppose tasks are just a way of breaking up a console script into smaller tasks. Which is more manageable, but the same can still be achieved with one console script and a method for each task.

    But each to their own ;)

  • cakebaker

    @Joel: Well, the advantage of using tasks over using multiple methods is that tasks can be reused in other shell scripts.

  • Diego

    Hey this is very cool, indeed, but i have been trying to find out how to use; if it is possible?, my models in app/models
    Has anyone tried to??

  • cakebaker

    @Diego: Hm, I don’t understand what you try to accomplish…

  • Diego

    @cakebaker: Hi there! thanx for your help, there aren’t much examples out there, what i am trying to do is the following, from a shell script, i would like to access some data stored in the DB, Lets say the POSTS model for example, trying to get innactive posts or something like that. I’ve tried looking in the ACL shell without much success.

  • cakebaker

    @Diego: You can use something like:

    $this->Post = new Post();


  • Debbie


    i want to write a console app which will do from cron stuff my web interface can do interactively. SO i don’t want my code in shells or tasks. i want my shells and my controllers to use the same stuff, the models and the components. unless my controllers can also run tasks, i would only write stuff in tasks which is only useful from the command line.

    so do you think controllers can also run tasks or the code should go in a component or a vendor class?

  • cakebaker

    @Debbie: I think it should be no problem to use a task inside a controller as long as you don’t use command line specific things in your task like reading user input from the command line. And you probably have to include the required files manually as it was not planned to use tasks in that way ;-)

    Good luck!

  • CakePHP : effectuer une “task” via une interface web - Damien Mathieu, développeur web

    […] ce genre de scripts. Il en existe déjà un résumant assez bien la chose sur cakebaker […]

  • CakePHP : exécuter une “task” via l’interface web - Damien Mathieu, développeur web

    […] de développer ce genre de scripts. Il en existe déjà un résumant assez bien la chose sur cakebaker […]

© daniel hofstetter. Licensed under a Creative Commons License