Search code examples
laravelscheduled-tasks

CronTab for logging not working in Laravel


I am testing a simple logger command to put in a crontab. When I run php artisan schedule:run command I am able to get the desired output at first. But the crontab to run everyMinute() does not work.

My Crontab entry is * * * * * cd /Users/bikrambhandari/My Documents/Laravel Projects/Migration/artisan && php artisan schedule:run >> /dev/null 2>&1

My scheduling code is as follows

protected function schedule(Schedule $schedule)
    {
        $schedule->command('visa:expire')
            ->everyMinute();
    }

For now the command php artisan visa:expire code will write a line in log file. I am on mac. And want to have log file written every minute.


Solution

  • Assuming your scheduling code is fine, I'd guess your cron job isn't being called properly due to the spaces in your directory path that you are CD'ing to.

    Change your crontab line:

     * * * * * cd "/Users/bikrambhandari/My Documents/Laravel Projects/Migration/artisan" && php artisan schedule:run >> "/Users/bikrambhandari/My Documents/crontab.log.txt" 2>>"/Users/bikrambhandari/My Documents/crontab-error.log.txt"
    

    The quotes will let the spaces NOT act as an argument delimiter, so the cd should succeed, and then php will kick off. I changed the redirect of output from /dev/null to a file - this is where you can look to see if your actual schedule function has issues. Once you know it is all working, change the redirect back to /dev/null

    Edit -

    Changed redirect again, to simplify it and generate separate log files.

    I'd also add a line to just touch a file so you know cron is really running -

     * * * * * touch /tmp/cron_is_running