zen of coding

Keeping your CakePHP shells quiet

Whenever working with shells, you most likely would like to provide some output or feedback to the user. This is especially useful when testing or observing a given shell’s work flow.
However, having your shell provide any output while it runs as a cron job (for example), is useless to say the least.

To me it seems like providing a simple -silent param would be a nice way to keep the shell quiet.

In order to achieve this we’ll override our parent’s out() method.

Let’s build a simple test shell, to see how it works:

class TestShell extends Shell {

    //the shell isn't silent by default...
    private $silent = FALSE;

    function startup () {
      if(isset($this->params['silent']) && $this->params['silent']) {
        $this->silent = TRUE;
      }
    }

    function dostuff() {
      $this->out(__('Does this work?', TRUE));
    }

    function out($string, $newline = TRUE) {
      if(!$this->silent) {
        parent::out($string, $newline = TRUE);
      }

      return FALSE;
    }

}

So, the first time we run the shell, like…

#cake test dostuff

We should see the output: “Does this work?”

Next, when we provide a -silent param, like…

#cake test dostuff -silent

The shell will exit quietly.

Notice that we check $this->params[‘silent’], which will be automatically set as an index of $this->params array (and set to “TRUE”), if one is passed via the command line.

P.S. Remember, that if you have initialize() method in your shell, the output from it will not be suppressed (given this example) as it runs before the startup() method.

  • jas

    how do you reason that having script output in a cron job is useless? Personally, I like a little email reminder when something fails.

    also, whatever happened to /path/to/script >/dev/null 2>&1 ?

    That said, this could be a great stub for providing verbosity levels.

  • @jas

    Having output on the screen has little to do with email notification, they are absolutely not mutually exclusive.

    The *nix syntax is fine, however we are talking about CakePHP and not *nix administration ;)

  • Dardo Sordi

    > Having output on the screen has little to do with email notification, they are absolutely not mutually exclusive.

    Actually, the cron daemon will mail you the output of cronjobs, so you can be notified when something goes wrong.

  • @Dardo Sordi

    If such setting is setup in crontab (which is not always the case), secondly the output you might want to have on the screen could (and probably should) be different from what you’d like cron to report. Perhaps using different verbosity levels is a better option, but, again, this really wasn’t the point of the original post :)

  • Howard

    Wasn’t aware it was this easy, i went into cake’s core file and stripped the welcome stuff, for cron purposes.

    Thanks for the tip tek!

  • @Howard

    No problem.

  • @howard: Just override the welcome message in your shell – no need to modify the core.

  • Trying to use not the Shell class as the base for my cake shell scripts but another class:

    TestShell extends BaseShell

    BaseShell extends Shell

    So I can put that functions like welcome and out() into that shell. I have like 10 shell scripts and do not want to modify that for every script.

  • @primeminister

    What about tasks?

  • @teknoid
    Ah you mean that all current shell scripts should be converted to tasks and one shell script activating them?

    Too much work for now. Need a base shell class ;)

  • @primeminister

    Fair enough ;)

  • Thank you for this, teknoid. I didn’t need it then, but it came in very useful just now! The comments left about overriding the _welcome method, in combination with this also came in quite useful! Awesome!

  • @Brendon Kozlowski

    Nice, good to hear.

  • Thanks for this, the _welcome override was useful.

    However in the 1.2.x version I have installed I was still getting a couple of linefeed characters coming up which was a pain when using the shell window for other work.

    I modified the cake/console/cake script to have a -q option which worked well for me…I wrote a note about it on the follow link if it might be useful to others :-

    http://wiltonsoftware.com/posts/view/shhhh-keep-quiet-cakephp-console

  • teknoid

    @Brett Wilton

    Thanks for sharing, but overriding the core is not a good idea. It would be be best to submit a patch with an enhancement ticket.

%d bloggers like this: