Search code examples
phplaravelscheduled-tasks

Laravel Tasks - Storing when last ran


I've made CRON Job using Laravel's task scheduling. But what I need is to store somewhere when that task was last ran,

Does anyone have any methods of how they store that and also, If Laravel outputs anything that can tell you when it was last ran?

Thanks


Solution

  • Not possible directly, however it is possible if you cache a date-time string on each run (either at the beginning or end of your script).

    Cache::rememberForever('name_of_artisan_task', function () {
        return now()->toDateTimeString();
    });
    

    The example shows using the Cache facade's ::rememberForever method to create a key/value of the last time the task was ran. As the name suggests, this is saved forever.

    You can easily retrieve this date and time using the cache() helper:

    cache('name_of_artisan_task');
    

    The con with this method is that if your cache is cleared, you will not longer have this stored.