Search code examples
phpcroncakephp-3.0cron-task

Multiple Jobs in One PHP file with Crunz


I'm using https://github.com/lavary/crunz to make my CakePHP 3.0 cronjob calls.

The readme says: "The idea is very simple: instead of a installing cron jobs in a crontab file, we define them in one or several PHP files, by using the Crunz interface."

I want to define all my cronjobs in only one PHP file, but all the examples I could get showed only one job per file.

Does someone knows how to do multiple cronjobs in one file using Crunz?


Solution

  • The Crunz\Schedule::run() method registers and returns a new event each time you call it, so you can create many tasks with many calls to run(). A rough example probably looks like:

    <?php
    // tasks/backupTasks.php
    
    use Crunz\Schedule;
    
    $schedule = new Schedule();
    
    // Register your first task
    $schedule->run('cp project project-bk')       
             ->daily();
    
    // Register another task
    $schedule->run('other-task taskparam1 taskparam1')
             ->hourly();
    
    return $schedule;