PHP code example of spatie / laravel-short-schedule

1. Go to this page and download the library: Download spatie/laravel-short-schedule library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / laravel-short-schedule example snippets


// in your routes/console.php file

use Spatie\ShortSchedule\Facades\ShortSchedule;

// this command will run every second
ShortSchedule::command('artisan-command')->everySecond();
    
// this command will run every 30 seconds
ShortSchedule::command('another-artisan-command')->everySeconds(30);

// this command will run every half a second
ShortSchedule::command('another-artisan-command')->everySeconds(0.5);

// in app\Console\Kernel.php

protected function shortSchedule(\Spatie\ShortSchedule\ShortSchedule $shortSchedule)
{
    // this command will run every second
    $shortSchedule->command('artisan-command')->everySecond();
    
    // this command will run every 30 seconds
    $shortSchedule->command('another-artisan-command')->everySeconds(30);
    
    // this command will run every half a second
    $shortSchedule->command('another-artisan-command')->everySeconds(0.5);
    
    // this command will run every second and its signature will be retrieved from command automatically
    $shortSchedule->command(\Spatie\ShortSchedule\Tests\Unit\TestCommand::class)->everySecond();
}

// in your routes/console.php file

use Spatie\ShortSchedule\Facades\ShortSchedule;

ShortSchedule::command('artisan-command')->everySecond();

// in app\Console\Kernel.php

protected function shortSchedule(\Spatie\ShortSchedule\ShortSchedule $shortSchedule)
{
    // this artisan command will run every second
    $shortSchedule->command('artisan-command')->everySecond();
    
    // this artisan command will run every second, its signature will be resolved from container
    $shortSchedule->command(\Spatie\ShortSchedule\Tests\Unit\TestCommand::class)->everySecond();
}

ShortSchedule::command('artisan-command')->everySecond();

ShortSchedule::command('artisan-command')->everySeconds(30);

ShortSchedule::command('artisan-command')->everySeconds(0.5);

ShortSchedule::exec('bash-command')->everySecond();

ShortSchedule::command('artisan-command')->everySecond()->withoutOverlapping();

ShortSchedule::command('artisan-command')->between('09:00', '17:00')->everySecond();
 

ShortSchedule::command('artisan-command')->between('21:00', '01:00')->everySecond();
 

ShortSchedule::command('artisan-command')->when(fn() => rand() %2)->everySecond();

ShortSchedule::command('artisan-command')->environments('production')->everySecond();
 

ShortSchedule::command('artisan-command')->environments(['staging', 'production'])->everySecond();
 

ShortSchedule::command('artisan-command')
   ->between('09:00', '17:00')
   ->when($callable)
   ->everySecond();
 

ShortSchedule::command('artisan-command')->everySecond()->runInMaintenanceMode();

ShortSchedule::command('artisan-command')->everySecond()->onOneServer();
bash
php artisan short-schedule:run
bash
cp ./vendor/spatie/laravel-short-schedule/src/Commands/ShortScheduleRunCommand.php ./app/Console/Commands/ShortScheduleRunCommand.php