Skip to content

Instantly share code, notes, and snippets.

@M165437
Last active November 27, 2022 12:20
Show Gist options
  • Save M165437/d7d8c31a9cbc8192adfc6c4f3d0d659b to your computer and use it in GitHub Desktop.
Save M165437/d7d8c31a9cbc8192adfc6c4f3d0d659b to your computer and use it in GitHub Desktop.
PHP Artisan command that lists all scheduled tasks: php artisan schedule:list (Credit: https://stackoverflow.com/a/35559970/2714126)
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
class ScheduleList extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'schedule:list';
/**
* The console command description.
*
* @var string
*/
protected $description = 'List when scheduled commands are executed.';
/**
* @var Schedule
*/
protected $schedule;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(Schedule $schedule)
{
parent::__construct();
$this->schedule = $schedule;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$events = array_map(function ($event) {
return [
'cron' => $event->expression,
'command' => static::fixupCommand($event->command),
];
}, $this->schedule->events());
$this->table(
['Cron', 'Command'],
$events
);
}
/**
* If it's an artisan command, strip off the PHP
*
* @param $command
* @return string
*/
protected static function fixupCommand($command)
{
$parts = explode(' ', $command);
if (count($parts) > 2 && $parts[1] === "'artisan'") {
array_shift($parts);
}
return implode(' ', $parts);
}
}
@larryx64
Copy link

Awesome! I made a slight adjustment to add next / previous run time, if anyone's interested :)

<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Cron\CronExpression;
use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;

class ScheduleListCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'schedule:list';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'List when scheduled commands are executed.';

    /**
     * @var Schedule
     */
    protected $schedule;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(Schedule $schedule)
    {
        parent::__construct();
        $this->schedule = $schedule;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $events = array_map(function ($event) {
            return [
                'cron'    => $event->expression,
                'command' => static::fixupCommand($event->command),
                'next' => $event->nextRunDate()->toDateTimeString(),
                'previous' => static::previousRunDate($event->expression)->toDateTimeString(),
            ];
        }, $this->schedule->events());

        $this->table(
            ['Cron', 'Command', 'Next Run', 'Previous Run'],
            $events
        );
    }

    /**
     * If it's an artisan command, strip off the PHP
     *
     * @param  $command
     * @return string
     */
    protected static function fixupCommand($command)
    {
        $parts = explode(' ', $command);
        if (count($parts) > 2 && $parts[1] === "'artisan'") {
            array_shift($parts);
        }

        return implode(' ', $parts);
    }

    /**
     * Determine the previous run date for an event.
     *
     * @param  string $expression
     * @return \Carbon\Carbon
     */
    protected static function previousRunDate($expression)
    {
        return Carbon::instance(
            CronExpression::factory($expression)->getPreviousRunDate()
        );
    }
}

@nvahalik
Copy link

Excellent! I updated this to show Closure/Job names: https://gist.github.com/nvahalik/110ee066a39d9f68f3b4b159f7acad6c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment