Question

Strange behavior with cron and laravel scheduler

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.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Accepted Answer

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:

    chkconfig haveged on
    systemctl start haveged```

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Interesting indeed. The strace tool would have definitely helped in pinpointing the lag. So if “php” is different from “/opt/plesk/php/7.1/bin/php”, I wonder what’s in plesk’s php that would cause such a lag. I was suspecting something was off with php hence my asking to at switch to 5.6 or 7.2 as that would imply skipping modules, ini settings, and other oddities that could be under 7.1.

I wonder what php version is the cli one:

which php php -v

Hopefully whatever the Plesk engineer did is a good long term fix.

Cheers

@unixynet I have fresh news:

Yesterday I have created a simple php script and named it test2.php

echo date(‘Y-m-d H:i:s’) . PHP_EOL;

As I’m using laravel and it needs PHP 7.1, my cron call php files with php 7.1 path: /opt/plesk/php/7.1/bin/php test2.php and not using the default 5.6 php cli: php test2.php

The interesting thing is that if I run from console it hits a moment that the system is lagged and stops execution, then resume it after some minutes… watch -n1 /opt/plesk/php/7.1/bin/php /var/www/vhosts/winnersmartialarts.com/adm/test2.php

But if I run with only php, never is stopped, even when the system is lagged… watch -n1 php /var/www/vhosts/winnersmartialarts.com/adm/test2.php

I have waited to a lag window (lasts about 3-5 minutes) and when using php 7.1 the system is stopped (I cant run any php script using the 7.1 path) BUT the system let me to run using only php….

Example watch -n1 php /var/www/vhosts/winnersmartialarts.com/adm/test2.php runs forever as it should…

Today an engineer from Plesk have accessed the server and he told me he can’t reproduce the “watch” test, mysteriously after he has logged in to the server, the issue disappeared and now the server is running smooth as it should (for now).

I’ll update if the issue is still going on.

Thank you for your time.

Can you try disabling opcache and also remove the opcache_get_status from disable_functions. Also, when you created the PHP script, is it really just one line of code and in no way referencing anything from the Laravel framework?

Try switching to php 7.2 or perhaps even 5.6 to see if this is configuration related. Also try this from the command line:

/opt/plesk/php/7.1/bin/php -v

Any warnings or errors?