2

I have attempted to create a cron job in my CakePHP 2.x application. But all of the resources I have read online seem to be either doing it completely different to one another with little consistency or explain it in very complex terminology.

Basically I have created the following file MyShell.php in /app/Console/Command

<?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');

    }

}

?>

And I want to say run this code at midnight every night.

What do I do next? As the next part really confuses me! I have read on the Book at: http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html that I should run some code in the terminal to make it do it at a certain time etc. And I can set these up using my hosting provider rather easily it seems.

But I'm rather confused about the Console directory. What should go in what folder in here: https://github.com/cakephp/cakephp/tree/master/app/Console

/Console/Command
/Console/Command/Tasks
/Console/Templates

Also noticed that many of the files are .php (e.g. my Shell file is also .php), but according to documentation I've read for Cron jobs, the executed files should be .sh?

Can anyone shed more light on this?

And what would the code be to call that command?

e.g. would presume this is incorrect: 0 0 * * * cd /domains/driz.co.uk/html/App && cake/Console MyShell sendEmail

Thanks

Cameron
  • 24,868
  • 91
  • 263
  • 449

2 Answers2

2

No. There is no way to do it just in PHP. But that doesn't matter, because crons are easy to set up.

In that article you linked to, you still have to set up a cron - the difference is just that you set up a single cron, that runs all your other crons - as opposed to setting up one cron per job. So, either way, you have to learn to create a cron.

The instructions depend on your server's operating system and also what host you're with. Some hosts will have a way to set up cron jobs through a GUI interface like cPanel or something, without you having to touch the terminal.

It's usually pretty easy to find instructions online for how to set up cron jobs with your host or server OS, but if you're having trouble, update your question with your host's name, and your server OS and version.

Also ---------------------------------

Often in cron jobs you'll be running a shell script (.sh). But don't worry about that for this case; your's will end in .php.

Re: the directory structure:

/Console/Command is where your new file should go.

If you're doing a lot of shell stuff, you may want to abstract common code out into the /Console/Command/Task folder. Read more about that here. This probably won't be needed in your case.

/Console/Command/Templates is where you can put custom templates for the Cake bake console - don't worry about that for now.

If I've only got a couple of cron jobs to run, then I create just one file called CronJobsShell.php, and put them all in there.

Really, you should read Cake's documentation on shells from start to end. It will give you a nice picture of how it all hangs together.

joshua.paling
  • 12,854
  • 3
  • 38
  • 57
  • How does software like WordPress do it then? As it's running them to auto-update itself and I'm not touching the terminal. – Cameron Oct 06 '13 at 23:41
  • Wordpress doesn't auto-update itself. It asks you if you'd like to update, and it does so when you're logged in to the admin interface. So, there's no cron running. When someone views an admin page, Wordpress can check it's current version against whatever the latest version is, and if it's not up to date, display a message asking if you want to upgrade. – joshua.paling Oct 06 '13 at 23:44
  • In v3.7 it auto-updates and sends an email every night with the log of the process. http://wordpress.org/news/2013/09/wordpress-3-7-beta-1/ – Cameron Oct 06 '13 at 23:44
  • WordPress simply triggers its Cron API on every request, so in case neither a visitor nor a bot or a user hits your page, the sheduled actions won't execute. For real, exactly timed cronjob behaviour you need to set up a real cronjob yourself: http://tommcfarlin.com/wordpress-cron-jobs/ @Cameron – ndm Oct 07 '13 at 00:45
  • OK... I haven't used Wordpress in a while. Still, my answer holds true. There's no way to run a 'real' cron through PHP alone. – joshua.paling Oct 07 '13 at 00:52
  • @ndm and this still applies in the latest developer build of WordPress 3.7? – Cameron Oct 07 '13 at 08:46
  • @Cameron Yes, it does. PHP doesn't come with a Cron API, you'd need to invoke external applications if you want to manage cronjobs with PHP, see for example http://stackoverflow.com/questions/4421020/use-php-to-create-edit-and-delete-crontab-jobs – ndm Oct 07 '13 at 12:52
  • @ndm Okay. Could you shed some light on the second part of my question. As I'm confused about the different directories in the console folder. And apparently the files that get executed by the cron, should be .sh and not .php? – Cameron Oct 07 '13 at 14:35
  • @Cameron It's better to have everything in a single answer, so you may want to let joshua.paling answer this, or create a separate question. – ndm Oct 07 '13 at 14:50
  • @joshua.paling Is this something you can answer? Thanks. – Cameron Oct 07 '13 at 15:28
  • Thanks for the answer. I'll accept it, as further questions I might have in the future regarding this, will likely be out of scope of the original question. However could you show a quick example of what I would write in the terminal to call my method in that shell file. – Cameron Oct 08 '13 at 15:38
  • If your shell was called CronJobsShell.php, you'd run "/path/to/your/app/Console/cake cron_jobs", or from within your app directory, just "./Console/cake cron_jobs" – joshua.paling Oct 08 '13 at 21:44
  • So how do I call the individual methods in the shell? As I presume that doing the above way calls all of them in sequence. Thanks again for the help. – Cameron Oct 09 '13 at 12:45
  • Read the documentation here: http://book.cakephp.org/2.0/en/console-and-shells.html#creating-a-shell for examples of both calling specific methods, and also passing in parameters to methods. – joshua.paling Oct 09 '13 at 21:48
  • Long story short: "/path/to/your/app/Console/cake cron_jobs my_method my_param" – joshua.paling Oct 09 '13 at 21:49
  • Cameron, I'm trying to implement the exact same cron job function (send a test email) but I can't for the life of me get it to work. What was your final solution? – bowlerae Apr 18 '14 at 12:10
  • Hello, i am getting this error when cron is excuted PHP Fatal error: Class 'App' not found in /home/skmaism/public_html/lfs/app/Console/Command/UsersUpdateShell.php on line 2 – Mr. Tomar Sep 04 '17 at 12:31
0

This can be done very easily by the following steps -:

1) Create a shell let's say HelloShell.php in Console/Command

 <?php
    class HelloShell extends AppShell
    {
    public function main()
    {
    //Your functionality here...
    }

    }

    ?>

This shell can be called by Console/cake hello

2) Write the command crontab-e .This will open up the default editor or the editor which you select Now as we want that our shell should run after at midnight write:-

0 0 * * * /PATH TO APP/Console/cake hello

For better understanding refer https://www.youtube.com/watch?v=ljgvo2jM234

Thanks!

er_zoya
  • 1
  • 1