How to setup cronjobs in cake php?

后端 未结 4 566
挽巷
挽巷 2020-11-29 11:37

How can I use cron job in cakephp to call an action of a controller on Ubuntu, I am trying to do it through crontab but it is not working?

相关标签:
4条回答
  • 2020-11-29 11:46

    Since we dont need the View layer of MVC, i will also suggest to use Cake shell instead of writing it in normal controllers. This will also reduce the memory load of various callback functions of controllers like beforeFilter, beforeRender et al.

    Then make an entry in **nix cron tab for crons written in your CakeShells.

    0 讨论(0)
  • 2020-11-29 11:52

    Use a shell

    The 'Cake Way' of using a CakePHP application in cron jobs would be creating shell and then calling it as a cron job.

    i.e. Create a shell to do the task, and then add it to crontab (crontab -e on linux machine):

    0 * * * *       cd /path/to/app/ && Console/cake your_shell_name params
    

    Creating shells and using them with cron is covered in the documentation.

    0 讨论(0)
  • 2020-11-29 11:57

    You can create a cron jobs calling the URL directly:

    php -f http://yoursite.com/yourController/yourAction/params > /dev/null
    

    Or you can do it also with relative paths:

    php -f CakePHP/yourController/yourAction/params > /dev/null
    

    You can take a look at how to use cron for unix here.

    Anyway, I would recommend you to use CakePHP Shell if the action is only going to be available from the cron jobs and not to any other user by URL.

    You have more info about how to to call Cake Shell from cron jobs in CakePHP documentation.

    0 讨论(0)
  • 2020-11-29 12:00

    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 every 5 minutes write:-

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

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

    3) If you want to call the action of the Controller in Shell simply import that particular controller let's say AppController by App::uses('AppController', 'Controller');

    Now create the object AppController in Shell by

    $object =new AppController();
    $object->func_in_controller();
    

    Now the parameters of function can be accessed in the Shell by $object->func_param;

    Thanks!

    0 讨论(0)
提交回复
热议问题