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

uccdev's avatar

Task scheduling runs every minute despite my directions

Hi,

I'm scheduling my app/console/Kernel.php to run the following code every fifteen minutes:

       protected function schedule(Schedule $schedule)
       {
               Log::info('now im attmept schedule');
               $schedule->job(new ServerJob("hi"))->everyFifteenMinutes();
               Log::info('i doon');
       }

The problem is, this isn't running every fifteen minutes, it is running every ONE minute.

The schedule calls the following job:

     public function __construct($serveJob)
     {
           Log::info("Job constructor");
           $this->serveJob = $serveJob;
           self::findBBDirect();
           self::testEmail();
           Log::info("job's dun");
     }

That job's functions look, right now, like:

    public function testEmail() {
          Log::info("Test email");
          Mail::send("testCurl", [],
          function($message) { //use ($filePath)
            $message->to('[email protected]', 'Artisans Web')
            ->subject('Marco Polo');
            $message->from('[email protected]', 'Sample name');
          });
        }

        public function findBBDirect() {
          $token = $this->accessToken;
          $headers = ['Authorization: Bearer ' . $token];

          $curl = curl_init();
          $validReturn = true;
          $url = "https://my.instructure.com/api/v1/accounts/1/courses?per_page=100";

          curl_setopt_array($curl, [
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLINFO_HEADER_OUT => TRUE,
            CURLOPT_URL => $url,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_SSL_VERIFYPEER => TRUE,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HEADER => TRUE,
            CURLOPT_RETURNTRANSFER, TRUE
          ]);
          if (curl_errno($curl)) {
            Log::info('Error:' . curl_error($curl));
          } else {
            Log::info('Yep, curl is fine');
          }

          $resp = curl_exec($curl);
          $header_size = curl_getinfo( $curl, CURLINFO_HEADER_SIZE );
          $header = substr( $resp, 0, $header_size );
          $body = substr( $resp, $header_size );
    
          $data = json_decode($body, true);
          $explData = explode("\n",$resp);
          $buttons = ["Current", "Next", "Prev", "First"];
          $links = $explData[15];
          $pageLinks = explode(",", $links);
          $bbData = array_fill(0, 4, null);
          $numBBs = 0;
          foreach ($data as $d) {
            if((array_key_exists("id", $d)) && (self::hasBBDirect($d["id"]))) {
              Log::info("found BB");
              $bbData[$numBBs] = $d["id"];
              $numBBs++;
            }
          }
          curl_close($curl);
    }

It may also be of interest that this was not running irregularly until recently. I don't know what change I made to cause this, but I can't seem to go back to it.

Any ideas why this would happen?

To clarify, I've seen this thread as well: https://laracasts.com/discuss/channels/general-discussion/task-scheduling-runs-every-minute-instead-of-the-time-that-i-set . But since I am not giving any specific cron directions, or usage, I am not sure the advice within is applicable here. I am extra unsure, in fact, since just two days ago, all was working as I had expected it to. That it isn't now, I don't know what to do.

Any help would be greatly appreciated

0 likes
7 replies
uccdev's avatar

Having investigated, I believe it's not due to any of the code I wrote here, but how I called my scheduler from the server itself:

              * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

I should change the number from "1" to another value, according to how many minutes I want to run it for.

MThomas's avatar

The idea is that the cron job is called every minute. And that you set the timeframe/interval on your jobs. If there is no job set in your code base for the moment the cronjob runs, no jobs will be executed. If there is a job set in the codebase for that moment, it will run.

Or have I completely missunderstood your problem?

Snapey's avatar

as @mthomas says, cron runs every minute, and your everyFifteenMinutes evaluates if the current minute is 00, 15, 30 or 45

The confusion is that your logging is in the part that runs every minute and not the job.

move your logging to the job itself then see how often it runs.

uccdev's avatar

@SNAPEY - @snapey Thank you. I have been logging in the job itself, and I can confirm that it is running every minute there. I also send an email to a test account with this schedule, and that has been going every minute too.

click's avatar

@uccdev that makes total sense because you do:

new ServerJob("hi");

and within the constructor of this class you log messages to a file and send a test email. The constructor of this class is being called every minute. So you are not scheduling anything at the moment.

Look into the documentation: https://laravel.com/docs/5.8/scheduling#introduction

  1. Create an artisan command that runs the job
  2. Create a callable like:
$schedule->call(function () {
            new ServerJob("hi")
})->daily();
uccdev's avatar

@CLICK - Click, thanks for your reply. But I'm still unsure. First, I'm not simply doing this:

       new serverJob("hi");

I'm doing:

       new serverJob("hi")->everyTenMinutes();

Yet it's still being called every minute.

Second, I've tried the callable exactly as you wrote it:

       $schedule->call(function () {
            $this->job(new ServerJob("hi"));
       })->daily();

yet...it doesn't seem to work at all. I get no initial call from it at all. I only get the log messages from outside the call, none of those inside the Job. It's like there's some error in the code. Not sure where to go from there?

EDIT: To be clear, in the logs, I get this error message:

                    local.ERROR: Call to undefined method App\Console\Kernel::job()
Snapey's avatar

The docs agree with your original code

$schedule->job(new Heartbeat)->everyFiveMinutes();

So, one heartbeat job queued at 00, 15, 30 or 45

On other minutes, no job queued, but some of logging in the original question would still occur (as already stated).

So, I'm no further forward. Just wanted to point out (to others) that $schedule->job() is fine as an approach

1 like

Please or to participate in this conversation.