Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting the Laravel cron job on a Mac

I have a command scheduled in the Laravel 5.4 scheduler and would like to start the Laravel cron on Mac OS X El Capitan.

app/Console/Kernel.php

<?php
namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    protected $commands = [
        'App\Console\Commands\GetToken'
    ];

    protected function schedule(Schedule $schedule) {
        $schedule->command('gettoken')->everyMinute();
    }

    protected function commands() {
        require base_path('routes/console.php');
    }
}

My GetToken.php makes an API call and then a DB change. I believe that this is working properly, as I can run the task directly from the cli using:

php /path/to/project/artisan schedule:run 1>> /dev/null 2>&1

To edit my cron file I use:

env EDITOR=nano crontab -e

I then add:

* * * * * php /path/to/project/artisan schedule:run >> /dev/null 2>&1

I save with ctrl+o and exit with ctrl+x.

Re-editing the file shows that the changes have saved.

Running crontab -l shows the text that I entered into the crontab file.

My cron never runs. I can only get it to run once by running manually using the command I mentioned above.

like image 859
Twentyonehundred Avatar asked Mar 21 '17 10:03

Twentyonehundred


1 Answers

Not directly answering your question, but proposing another solution:

If you want to set up cron jobs for your development environment, it's best to use Homestead, for its Linux standards compliance.

For small projects that i develop directly inside macOS, i run the following command inside the project root (in a separate terminal tab) to have my jobs run every minute:

while true; do php artisan schedule:run; sleep 60; done

This helps to make sure, the cron jobs are only run while i'm developing. When i'm done, i Ctrl+C that command and can be sure nothing unexpected happens while i'm not watching.

Plus it gives me the freedom to adjust the interval, by simple choosing another number of seconds for the sleep command. This can save time when developing.

Update Laravel 8.x

Laravel now offers the above as a single artisan command:

php artisan schedule:work
like image 67
jsphpl Avatar answered Oct 19 '22 18:10

jsphpl