Search code examples
phplaravelschedulelaravel-artisan

Disable a scheduled job


I have a live PHP/Laravel project with multiple scheduled jobs, The customer asked for one of the jobs to be disabled, is there a way to disable ONE of the scheduled jobs through the command line?

I, of course, can comment out the code in the console\kernel.php which registers the task but this is only temporary and i don't want to make a code change and release just for this one task.

+-------------+-----------------------------------------------+
| Cron        | Command                                       |
+-------------+-----------------------------------------------+
| 0 23 * * 7  | 'artisan' export:blahblah                  |
| 0 1 5 * *   | 'artisan' invoices:somejob          | <---- i want to disable just this one job
| 30 1 * * *  | 'artisan' invoices:blahblahblah         |

Solution

  • You could configure a global variable, or database flag, that will be checked everytime at the beginning before running command logic.

    For example:

    <?php
    
    class InvoicesCommand {
    ...
    
    // handles invoices:somejob command
    public function handle() {
        // get command flag (can be set from .env too, or however you want)
        $invoicesCommandFlag = Configuration::where('key', 'invoices_command_flag')->firstOrFail();
    
        // check if command is "enabled"
        if (!$invoicesCommandFlag->value) {
            \Log::debug('Command disabled. Aborting.');
            return;
        }
    
        // do stuff...
    }
    ...
    }