Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

simonw's avatar

Laravel schedule cron() problem

Hey all,

I'm having trouble scheduling jobs from a task.

$this->schedule->job(new MyJob, 'default')->cron('0 0 15 12 *');

One would think this would schedule a job to run at midnight, 15th December.

However I can't see this Job appearing in the jobs table. So I assume it won't appear until that time.

Then I wondered if I can see the list by simple doing a dd($schedule-events()) . It gives me an empty array [].

Is this expected? How can I see my pending schedule job? crontab -l doesn't help me either.

0 likes
8 replies
ismaile's avatar

Do you use the sync queue connection ? It is defined in the .env file.

rodrigo.pedra's avatar

However I can't see this Job appearing in the jobs table. So I assume it won't appear until that time.

You're correct, right now Laravel does not have a way to list scheduled jobs.

If your server is configured correctly as per documentation to run php artisan schedule:run on every minute your job should be fired on the right time.

There is an open issues on the laravel/ideas repository requesting to add a schedule:list command:

https://github.com/laravel/ideas/issues/1320

In that issue there is a link to a package that provide a command to list scheduled commands:

https://github.com/hmazter/laravel-schedule-list

For reference here is the documentation on how you should configure your server to run the php artisan schedule:run on every minute:

https://laravel.com/docs/6.x/scheduling#introduction

Scroll to the "Starting The Scheduler" section.

simonw's avatar

Thanks so far :) Let me elaborate on the exact code a little to see if it helps at all.

I have a scheduled task that runs once a day. To test, I run it manually on the cmd line using php artisan userm:recur. That task looks up records in the database, and loops over it, creating several new scheduled jobs.

Essentially userm:recur is a job manager that checks once a day to see if any new records are in the database table, and if there are, schedules any new jobs based off information in this table.

    ...

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

        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $newSchedules = $this->repo->allNew();

        foreach ($newSchedules as $newOrder) {
        // returns a Carbon date instance of when this job should be scheduled for
            $date = $newOrder->calculateNextRun();

            // https://www.adminschoice.com/crontab-quick-reference
            $crontab = [
                $date->minute, // minute
                $date->hour, // hour
                $date->day, // day of month
                $date->month, // month
                '*'// day of week
            ];

            $cronstring = implode(' ', $crontab);

            // explode crontab command
            $this->schedule->job(new MyJob, 'default')->cron($cronstring);

            $newOrder->next_run_at = $date;
            $newOrder->save();
        }
    }

I use info in that table to figure out the date of the next schedule, then create a crobtab command, and this is what bought me to Laracasts. I cannot see evidence of this job being created.

I tried dd($schedule->event()) which I thought was supposed to get me the list of things currently scheduled. Am I wrong to assume that?

Is the Schedule instance correct? dd($schedule) does show me an instance of Illuminate\Console\Scheduling\Schedule so I'd assume so.

simonw's avatar

@ismaile

BROADCAST_DRIVER=log 
QUEUE_DRIVER=database

I'm already creating jobs elsewhere in the system.

I use php artisan queue:watch to ensure the queue is running. I restart the queue everytime I make a change to the job. But the job here is irrelevant if the scheduler isn't even scheduling it!

simonw's avatar

@rodrigo.pedra I can attest that the scheduler on my local is not configured, however I run the top level task manually when testing anyway.

Does the scheduler need to be running to schedule jobs using ->cron() I thought if I ran the manager as in my code above, $schedule->jon()->cron() would still work as it's a preset time, and not checking to run every minute, and would still add them to the queue.

rodrigo.pedra's avatar
Level 56

Hmm, so I tried the following code:

resolve(Illuminate\Console\Scheduling\Schedule::class)->events();

And it indeed lists all the scheduled commands.

But by reading your later posts I got a better picture on your problem.

In your job you are calling $this-schedule->job(...)->cron(...) hoping that Laravel will persist on the scheduler to be ran later, right?

Unfortunately this is not how it works :(

The schedule:run run command will only check for commands defined in the schedule method on your Console\Kernel file. You can see this statement on the docs:

https://laravel.com/docs/6.x/scheduling#defining-schedules

You may define all of your scheduled tasks in the schedule method of the App\Console\Kernel class.

So when you inject the Schedule object in your job and try to schedule a new job through it, nothing happens in practice.

To add jobs dynamically you could create a Command that runs every minute that checks a table for future jobs and run them on your expected time.

Luckily there is a package for that:

https://github.com/codestudiohq/laravel-totem

This package provides a dashboard so you can monitor (add/edit/delete) scheduled jobs.

As you need to add jobs programmatically take a look in its TasksController and TaskRequestto add new scheduled tasks manually.

simonw's avatar

@rodrigo.pedra thank you. I'll run have to figure a way around. Maybe Totem is what I need. I also like the less bulk approach of running the Job at the time it needs to, rather than pre-scheduled with cron.

I'll see what we need in practice and work out which will fit best.

It's weird that commands can ONLY be scheduled from Kernel. Seems like a limitation? The docs suggest that you "may define" them in Kernel but I read that loosely as, "also maybe not, and so could define elsewhere"

Thanks for the help.

1 like
rodrigo.pedra's avatar

You're welcome

There is an open discussion on laravel/ideas repo about allowing a job to define its own schedule.

https://github.com/laravel/ideas/issues/1876

Most of the arguments are around third-party packages staring firing scheduled jobs without your knowledge.

So I guess it is not something we will have soon.

Please or to participate in this conversation.