Strange behavior with cron and laravel scheduler

Posted September 29, 2018 16.2k views
CentOSApplicationsLinux Commands

Hello, I have configured a cron job with plesk in my droplet to run a laravel command every minute
/opt/plesk/php/7.1/bin/php /var/www/vhosts/website/laravel/artisan schedule:run
This executes the Laravel scheduler, that is filled with tasks to run at certain time, lets say send emails at 10:00 and send SMS at 11:00

So when the cron runs the scheduler and a task is found at the same time it executes the task, eg
9.58 no tasks
9.59 no tasks
10:00 send emails
10:01 no tasks

My problem is that FOR SOME STRANGE REASON the scheduler is delayed at some times, so the problem is like this
9.58 no tasks
9.59 no tasks
10:01 no tasks
10:01 no tasks

If I run this command from command line, it runs instantly, with no delay. But for some reason when this is executed from the cron, sometimes is delayed. I have tested the cron from inside plesk, there is a button Run now and is true, for some reason sometimes it takes about 65 seconds to run the cron job or the command artisan schedule:run

Any ideas?
This only happens in the droplet I have, not in local environment.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
17 answers

The issue was caused by depleted entropy by PHP script.

To resolve the issue generate entropy by using haveged daemon:

  1. Enable EPEL repository:

    yum install epel-release

  2. Install daemon with the following command and enable it:

    yum install haveged
    chkconfig haveged on
    systemctl start haveged

The delay is typically due to a high load, deep run queue, or perhaps clock drift. On a busy server, tasks that are forked will likely incur some delay at startup (versus daemons/services that are “hot”). How busy is this box (Run: uptime; sar -q; etc)? How many tasks do you typically run at the minute mark?

Also, make sure your clock is sycned up (ex: ntpdate -s

Hi @unixynet I don’t think is the case of a busy server, this is the only laravel app running, the app is private so not many users are at the same time.
The results of running uptime; sar -q; etc are 10:52:05 up 4:29, 1 user, load average: 0,02, 0,04, 0,05

I have no tasks at the minute mark, only 3 tasks per day on different times… The clock is synced so this is realy strange… Any more ideas for me to try?

What are the timestamps of the cron runs in the logs? Does it reflect the 1sec delay you’re seeing in your app?

@unixynet I’m sorry but is not a 1 sec delay, is a 1 minute delay, sometimes a 3 minutes delay.
The cron is run on time by the logs, but for some reason the artisan command is not executed on the same time that the cron runs, is like it takes some time to load so when the scheduler loads is not the same time that the cron was executed eg the cron runs at 10:05:01 -> takes time to load -> scheduler loads at 10:06:11 and skips the task that needs to execute at 10:05

cron log

Sep 29 10:05:01 localhost CROND[17311]
Sep 29 10:10:01 localhost CROND[17846]
Sep 29 10:15:01 localhost CROND[18165]

Scheduler log

Received ping at Saturday, 29-Sep-2018 10:06:11 EDT
Received ping at Saturday, 29-Sep-2018 10:10:01 EDT
Received ping at Saturday, 29-Sep-2018 10:18:55 EDT

Cron looks OK. How’s your DB? Can you enable the slow log to see if any queries are slowing things down and thus affecting event clearing?

@unixynet I can enable the slow log but I don’t think that DB is playing here as the only task on scheduler is not involving any DB query.

The only task I have is:

$schedule->call(function () {
            return true;

I will enable the slow log and return here.

@unixynet Slow query log does not fill up, so no slow queries.

But I have fresh results:

I have discovered that if I execute any artisan command during the “hang up” it takes forever. Lets say you run any artisan command at 16:24 this command is not resolved until 16:27:31 (when the system is “locked out”)

I have no more ideas… Have looked in logs and all that but NO CLUE. I think is not Laravel, maybe is a server issue. Server load is really little, is a droplet for a private app with 15 users.

Cron log:

Sep 30 16:20:01 localhost CROND[28616]
Sep 30 16:21:01 localhost CROND[854]
Sep 30 16:22:02 localhost CROND[988]
Sep 30 16:23:01 localhost CROND[1062]
Sep 30 16:24:01 localhost CROND[1132]
Sep 30 16:25:01 localhost CROND[1199]
Sep 30 16:26:01 localhost CROND[1255]
Sep 30 16:27:01 localhost CROND[1315]
Sep 30 16:28:01 localhost CROND[1411]
Sep 30 16:29:01 localhost CROND[1508]
Sep 30 16:30:01 localhost CROND[1599]
Sep 30 16:31:01 localhost CROND[1670]
Sep 30 16:32:01 localhost CROND[1742]
Sep 30 16:33:01 localhost CROND[1807]
Sep 30 16:34:01 localhost CROND[1874]
Sep 30 16:35:01 localhost CROND[1936]
Sep 30 16:36:01 localhost CROND[1995]
Sep 30 16:37:01 localhost CROND[2062]

Scheduler log:

2018-09-30 16:20:12] production.INFO: schedule call  
[2018-09-30 16:21:01] production.INFO: schedule call  
[2018-09-30 16:22:02] production.INFO: schedule call  
[2018-09-30 16:23:01] production.INFO: schedule call  
[2018-09-30 16:27:31] production.INFO: schedule call  
[2018-09-30 16:27:31] production.INFO: schedule call  
[2018-09-30 16:27:31] production.INFO: schedule call  
[2018-09-30 16:27:31] production.INFO: schedule call  
[2018-09-30 16:28:01] production.INFO: schedule call  
[2018-09-30 16:29:01] production.INFO: schedule call  
[2018-09-30 16:30:01] production.INFO: schedule call  
[2018-09-30 16:31:01] production.INFO: schedule call  
[2018-09-30 16:32:02] production.INFO: schedule call  
[2018-09-30 16:33:01] production.INFO: schedule call  
[2018-09-30 16:37:53] production.INFO: schedule call  
[2018-09-30 16:37:53] production.INFO: schedule call  
[2018-09-30 16:37:53] production.INFO: schedule call  
[2018-09-30 16:37:53] production.INFO: schedule call  

A cron job entry like this:

  • * * * * /usr/bin/php /home/httpgun/artisan schedule:run

Will definitely make several SQL queries. But let’s just remove any doubt and repair all tables:

mysqlcheck -r db_name

With this out of the way, have you tried the elimination approach? Remove all schedule calls and add one at a time to see where delay is introduced. If there’s delay with zero cron job, there’s a big problem with core.

Hi @unixynet, I have disabled the Laravel scheduler cron and create a simple cron with a php file that records time of execution, and the problem seems to still be there.. I’m thinking this is more of a server issue than a laravel. See results:

Cron logs

Oct 1 10:01:01 localhost CROND[2315] 
Oct 1 10:02:01 localhost CROND[2388] 
Oct 1 10:03:01 localhost CROND[2440] 
Oct 1 10:04:01 localhost CROND[2493] 
Oct 1 10:05:01 localhost CROND[2566] 
Oct 1 10:06:01 localhost CROND[2627] 
Oct 1 10:07:01 localhost CROND[2743]

PHP file execution logs

Monday, 01-Oct-2018 10:00:01 EDT 
Monday, 01-Oct-2018 10:05:42 EDT 
Monday, 01-Oct-2018 10:05:42 EDT 
Monday, 01-Oct-2018 10:05:42 EDT 
Monday, 01-Oct-2018 10:05:42 EDT 
Monday, 01-Oct-2018 10:05:42 EDT 
Monday, 01-Oct-2018 10:06:01 EDT 
Monday, 01-Oct-2018 10:07:01 EDT
Previous 1 2 Next