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

bionary's avatar

Schedule Runs Manually but Not With Cron as Expected

I have a bizarre problem that I would like a few brains to help me figure out the solution. (I'm sure I'm just overlooking something trivial like usual)

I have setup laravel cron scheduling in the past with success on previous projects. This time around I have a complex web scraping routine that works fine when run manually but does not run via automatic scheduling.

It's so weird.

I set up the cron job and put in simple test:

        $schedule->call(function () {
            DB::table('test')->insert(['key'=>date('Y-m-d H:i:s') ]);
        })->name("scrape test")->everyMinute();

The previous code works fine. I can see the database rows stacking up each minute...great

When I swap out the test for the real code like this:

        $schedule->call(function () {
            $tasksController = new TasksController;
            $tasksController->scrape('something');
        })->name("scrape test")->everyMinute();

It doesn't work :(

Now here is the baffling part...

If I run the scheduler via the command line it works fine!

php artisan schedule:run

My code works flawlessly when run via command line but not when called via the scheduler.

???

I'm out of ideas, this makes zero sense to me.

Have any ideas?

Thanks

ps. I can concur all of this via telescope as well. Telescope records each schedule when it's working and does not register the schedule even when it's not working

0 likes
3 replies
bobbybouwmann's avatar

How did you set up the cronjob on your server? It should be something like this

* * * * * cd /www/my-project && php artisan schedule:run >> /dev/null 2>&1

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

The reason that works when you run it manually, is because everyMinute always matches with the current time. It sounds to me that the cronjob is not running properly here.

bionary's avatar

Thank you for your reply. The cron is firing every minute. I can confirm this with the following block which works as expected:

   $schedule->call(function () {
        DB::table('test')->insert(['key'=>date('Y-m-d H:i:s') ]);
    })->name("scrape test")->everyMinute();

It really is strange.

bionary's avatar
bionary
OP
Best Answer
Level 6

So I spent yet a few hours sleuthing this out. I was sprinkling DB::table('test')->insert(['key'=>'some location']); statements every step of the way.

I also took a look at the error logs. (Hand over face moment) yes the logs. I didn't think to check them because I was so darn fixated on telescope's exception panel, which showed nada.

So I was able to resolve the problem.

I have a bunch of abstract classes that inherit from one another. Since this is a complicated project I have been type-hinting all inputs so that a few years from now, code maintenance is easier.

Well it turns out that a few inherited methods where type hinted in the abstract parent class BUT where not properly type hinted in the child class.

So in essence I had things like this in the Parent Class:

/* Abstract child methods */
abstract protected function encodeQuery(string $rawQuery);
abstract protected function cleanLinksHook(array $links);

But in the various child classes I had a few spots where I was sloppy and was creating the method without proper type-hinting... like this:

protected function cleanLinksHook($links) {
    ...
}

or this

protected function cleanLinksHook(Array $links) {
    ...
    //notice the capital A
}

I can see why this didn't work BUT the weirdest thing, is the code ALWAYS worked when running php artisan schedule:run once off, manually in the command line BUT never when run via cron/scheduler. I tested it dozens of times and there is not logical reason I can think of why this was the case BUT it was. very very very strange.

Please or to participate in this conversation.