phpcakephpcronmediatemple

Running CakePHP Cron jobs on Media Temple grid service


I have the following Shell in my CakePHP app:

<?php 

class MyShell extends Shell {

    public function sendEmail() {

        App::uses('CakeEmail', 'Network/Email');

        $email = new CakeEmail();

        $email->from('cameron@driz.co.uk');

        $email->to('cameron@driz.co.uk');

        $email->subject('Test Email from Cron');

        $result = $email->send('Hello from Cron');

    }

}

?>

Which I am trying to run every 5 minutes as a test on my Media Temple Grid Service host.

/home/####/domains/driz.co.uk/html/app && Console/cake my_shell send_email 

cd /home/####/domains/driz.co.uk/html && Console/cake my_shell send_email 

cd /home/####/domains/driz.co.uk/html/app && Console/cake my_shell send_email 

php /home/####/domains/driz.co.uk/html/app/Console/Command/MyShell.php

I have tried ALL of the above but none of them work... Any ideas why?

(Note: that the times are missing as you specify these in the admin panel separate to the actual command)

Has anyone got any experience running CakePHP cron jobs on Media Temple? I have asked MT support, and they explained that the first 3 lines wouldn't work because they are not direct commands to the files (so apparently the CakePHP docs are for the terminal only).


Solution

  • Here's what I did to get it working:

    Modified your email script to include some output:

    class MyShell extends Shell {
        public function sendEmail() {
            App::uses('CakeEmail', 'Network/Email');
            $email = new CakeEmail();
            $email->from('user@domain.com');
            $email->to('user@domain.com');
            $email->subject('Test Email from Cron');
            $result = $email->send('Hello from Cron');
            $this->out('Email sent successfully.');
        }
    }
    

    Next, I tested via console to ensure it would work. For the above code, the "Shell" suffix in "MyShell" is omitted per the docs and the second parameter is the function name. Here was the CLI command that worked for me from the root of the application, /home/#####/domains/mydomain.com/html/cake/cakephp/app.

    Console/cake my sendEmail
    

    Output:

    Welcome to CakePHP v2.4.1 Console
    ---------------------------------------------------------------
    App : app
    Path: /nfs/c##/h##/mnt/#####/domains/mydomain.com/html/cake/cakephp/app/
    ---------------------------------------------------------------
    Email sent.
    

    My value in the command box in the MT (gs) dashboard was constructed according to this. It includes:

    cd 'app_root' && Console/cake 'class_name_sans_shell' 'function_name'
    

    For the above code, that results in:

    cd /home/#####/domains/mydomain.com/html/cake/cakephp/app && Console/cake my sendEmail
    

    It's worth noting that "Console/cake" is our executable in this case so that's the program we are running.