Help, my host does not support CRON jobs


If you are working on a shoestring and using a shared host for your latest Laravel 5 project, you may want to setup some scheduled jobs. Laravel 5 has a great scheduler built in, but it needs a kick every minute for it to determine if it is time to run the job.

Through a third party service such as cron-job.org it is possible to provide this kick to the Laravel 5 Scheduler.

1. Create an account at cron-job.org

Accounts are free and permit you to schedule a task as frequently as once per minute

2. Create a route in your application to kick the laravel scheduler

    //trigger the scheduler
    Route::get('/hshhdyw7820037lammxh29' , function(){
        Artisan::call('schedule:run');
        return 'OK';
    });

Here I have used a random string for the path so that it is not accidentally ‘found’. If it would be an issue if your task is triggered twice, you might want to protect it further such as checking for the request coming cron-job.org’s IP address.

3. Add this route to the cron-job.org schedule

cron-tab

 

Other thoughts

By triggering the scheduler this way rather than just running the job directly means that you can then use the power of the Artisan scheduler.  Check out Eric Barnes intro to using the scheduler.

One thing not covered by Eric or the documentation is the ability to run a task every few minutes (5 minutes is catered for).

This example runs the ReplayServiceProvider every two minutes.

    $schedule->call('App\Providers\ReplayServiceProvider@feedData')->cron('*/2 * * * *');

Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Help, my host does not support CRON jobs