How to start a cron job using schedule:run manually in Laravel 5 (October CMS actually)

Viewed 12526

I know how to run cron jobs on Linux server, I know how to use $schedule->command('foo')->daily(); I have read this document many times https://laravel.com/docs/5.0/artisan

But my question is where shall I write this line exactly $schedule->command('foo')->daily();? and what is $schedule variable actually? I mean is this any predefined variable in parent classes to which we can directly call if not which class we shall include in our file and how to instantiate this $schedule object.

My main concern is what is the elegant way to schedule jobs, what is the point of writing this in code where i am writing my logic because this line should run once in lifetime if I am not wrong because this will then push the job in Jobs table and then * * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1 should take care of it for rest of the lifetime

then where exactly I shall write such code which will run only once while we deploying the application on the server before starting the main cron schedule:run.

Please, someone guide me why there is no written document for how to run a scheduled task manually without writing it in a code which will be called many time during execution of a real world web application.

3 Answers

Three Steps to take:

1) Set up cron task
http://octobercms.com/docs/setup/installation#crontab-setup

Cron will call the command scheduler every minute. Then October evaluates any scheduled tasks and runs the tasks that are due.

2) Make your console command
http://octobercms.com/docs/console/development
This command can be executed manually on the command line. And can be executed on scheduled time as explained next.

3) Register a schedule for the newly made console command in the Plugin registration class
http://octobercms.com/docs/plugin/scheduling#defining-schedules

class MyPlugin extends PluginBase
{
    [...]

    public function registerSchedule($schedule)
    {
        $schedule->command('cache:clear')->daily();
    }
}

Hope this helps!

  1. Upgrade to Laravel 8 (that is how I tested it)

  2. run php artisan schedule:list - see what you have

  3. run php artisan schedule:test - you will be given options

    Which command would you like to run?:      
      [0] App\...
      [1] App\...
    

then you type a number and see it is working

Related