I’ve been having problems while trying to setup a simple cronjob.
A lot of the WordPress tutorials like this one http://tommcfarlin.com/wordpress-cron-jobs/ specify a crontab with some time schedule and in WordPress they specify another time schedule. Can’t seem to understand that.
In the tutorial is specificed every 15 minutes:
*/15 * * * * wget -q -O – http://yourdomain.com/wp-cron.php?doing_wp_cron
and then in the code they say:
wp_schedule_event( time(), 'hourly', 'my_hourly_event' );
So this cron is going to run every 15 minutes or every hour?
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Well, it isn’t the best tutorial I’ve came across, so I get why you’re confused…
Smashing Magazine has a little bit better tutorial on this subject.
Some quotes from this article that should help you understand how does WP Cron work…
WordPress Cron is what many people refer to as a “pseudo-cron system.” The difference is in how UNIX cron and WordPress Cron take action. A typical UNIX cron system runs in this order:
- A time tied to an action occurs.
- Cron runs the action tied to that time.
With WordPress Cron, it works a little differently:
- A visitor comes to any page on your WordPress website.
- WordPress Cron checks each cron event to see whether the scheduled time has passed.
- If the scheduled time for that event has passed, then WordPress Cron executes any actions tied to that event.
So WP Cron is not the same as UNIX cron, but its purpose is very similar.
The biggest problem with WordPress Cron is that it depends on visits – if there are no visits on your site, WP Cron won’t run). That is why some tutorials tell you, that you should set up server’s cron to hit
wp-cron.php (it will force WordPress’ cron to run).
So why the times are different?
Let’s assume that:
- you scheduled an hourly action in WordPress Cron,
- last time it ran was at 2:15,
- your server’s cron runs 5 minutes after every hour.
What will happen if there will be no visits on our site from now on?
- at 3:05 server’s cron forces WP cron run – but WP cron won’t do anything (it should run hourly and it’s only 50 minutes since last run)
- at 3:15 WordPress cron won’t run – there are no visits
- at 4:05 server’s cron again forces WP cron run – now it will do the work.
But as you can see, even that this event is scheduled to run hourly, it haven’t been fired for almost 2 hours. Setting server’s cron to run more often will shorten this delay.