2

I'm using wp_schedule_single_event to run a function in the background immediately when a logged in user visits the dashboard page. The problem I'm having is that the time between triggering and the function actually running varies between a few seconds and 5 minutes.

I'm debugging by emailing myself as soon as I call wp_schedule_single_event, and then in the function that is called. I know the event is scheduled immediately because I get the email quickly, _transient_doing_cron is created in the wp_options table, and the name of my action is added to cron in wp_options while it needs to be run.

My function runs fairly quickly and is done in less than 10 seconds. It's as if wordpress is running other crons before it, but I've tested this multiple times only a few minutes apart, so I can't always be getting an hourly cron that needs to be run. Can I prioritize my event to be run first? What else could be happening?

  • Hi, welcome to WPSE. It'll be helpful if you provide the actual CODE involving the calling and implementation of wp_schedule_single_event. So it's better if you edit your question and provide the CODE. – Fayaz Apr 20 at 6:39
1

Cron execution may differ depending on when the system cron is actually being run. For example, if your system is running cron at 5 minutes interval, even a cron scheduled for immediate run will execute between a few seconds to 5 minutes.

You may use the WP Crontrol Plugin to check the cron schedules set by WordPress in case other cron tasks are affecting it. To check system cron schedules, use crontab command or check cron settings from CPanel.

Also, if you don't have define('DISABLE_WP_CRON', true); set in wp-config.php, and there is no Operating System cron running, that means WordPress is running the crons. In that case cron event execution will depend on site visits. However, from your following description:

the function actually running varies between a few seconds and 5 minutes.

it looks like an Operating System cron is running at 5 minutes interval. You may make it one minute interval to get faster execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.