Search code examples
laravelcronlaravel-artisan

How to know when an artisan command was last executed?


I've scheduled one command to be executed daily. But then, I had to move my application from one server to another. Now, I forgot if I had created a parent cronjob to execute schedule:run command.

Now, I've added the parent command but I want to when my artisan command was executed latest and confirm that I forgot to add parent artisan command.

Every time my application sends an e-mail, it is recorded and I've checked E-mail logs but my application sends very fewer E-mails on the daily basis which makes it impossible to see the change in graphs (In other words, it's not certain) and to know if I really forgot to add cronjob to execute parent command.

From now, how can I log the timestamp of latest execution of the command so that I or anyone does not have to face this issue every time (Obviously, If your application has scheduled commands, You just have to add one to as cronjob and forgot it which makes it harder to remember when you are transferring server after months).


Solution

  • You could just log the action when the command is executed:

    In the handle() method of your command class:

    namespace App\Console\Commands;
    
    use Illuminate\Support\Facades\Log;
    use Carbon\Carbon;
    
    class SomeCommand extends Command {
    
        // some code
    
        public function handle()
        {
            Log::info('SomeCommand has been executed at: ' . Carbon::now());
    
            // the rest of your code
        }
    }