According to codex,
Schedules a hook which will be executed by the WordPress actions core on a specific interval, specified by you. The action will trigger when someone visits your WordPress site, if the scheduled time has passed.
Assume that I run a function using wp_schedule_event
. Function need 20-30 seconds to finish their task.
So it will affect to the user or not? I mean if it is run in background like normal cron jobs it is not affect to the user.
According to codex,
Schedules a hook which will be executed by the WordPress actions core on a specific interval, specified by you. The action will trigger when someone visits your WordPress site, if the scheduled time has passed.
Assume that I run a function using wp_schedule_event
. Function need 20-30 seconds to finish their task.
So it will affect to the user or not? I mean if it is run in background like normal cron jobs it is not affect to the user.
Share Improve this question asked Oct 27, 2016 at 7:08 DamithattDamithatt 651 silver badge5 bronze badges 1- I'm not clear with your question. But, there are plugins like Advanced Cron Manager that can help you assign, display, debug scheduled events. "The action will trigger when someone visits your WordPress site, if the scheduled time has passed." - it means that, wp_cron runs only on user visit, not on server timing (in background). – Mayeenul Islam Commented Oct 27, 2016 at 8:10
2 Answers
Reset to default 3It depends. The initial page load trigger should not but performance may degrade as visitors browse your site until the Cron job is finished.
Is the scheduled event you plan on using PHP or DB intensive? How often will it run?
Here's how it works
- The scheduled event (aka Cron job) will be initialized once it's due (or past due) by whoever hits your website. So visiting
www.test/somepage/
will tell Mr. WP-Cron, "Hey dude, it's time to wake up and run this Cron job". - If it takes 20-30 seconds to finish and is an intensive DB or PHP task (e.g. lots of DB INSERT or UPDATE statements, or parsing a huge
.xml
file, etc), there's a good chance the site will be slow for anyone during that time. Especially if you're on shared hosting due to shared CPU and memory.
If it's a Cron job you run once a day or week, I wouldn't worry about the performance hit.
Supporting Answer
I think the question asks if any cron job runs in the "main" PHP worker, the one which is presenting output to the visitor, or it runs in another PHP worker, without "blocking" the visitor thread.
It's easy to find out by experiment.
Try to add an " echo '<h1>Yes, it Runs on this thread</h1>';
" statement somewhere in the start of the cron-job script.
If you visit your site then, and you see "Yes, it Runs on this thread" somewhere on your display, then the cron-job runs on your thread.
If you don't, which by the way is what really happens, the cron-job runs in the background (ie in another PHP "worker" when you are using PHP-FPM).