Using cron jobs to import your iCal feeds

Event Organiser Sync lets you pull in events from any number of feeds, and to specify the frequency with which this is done. It relies on “WP Cron” to do this and requires no server configuration.

WP Cron is a form of pseudo-cron that WordPress uses (e.g. to publish scheduled posts). It allows you to approximately schedule jobs. The available frequencies are quite limited, but you can register more schedules. However, it is not particular accurate.

Since Event Organiser Sync 2.1 you can also manage, validate and sync your feeds from the command line. This opens up the possibility of creating a genuine cron job to pull in your events at very particular times. For instance you may want feeds to be pulled in at 2am every morning, or to have different feeds synced at different frequencies.

Prerequisites

Before you begin, you shall need WP-Cli installed. Event Organiser Sync (2.1+) exposes a number of commands through WP-Cli and these can be listed as follows:

 wp eo ical-feed --help

We shall be using the fetch command.

At this point, if you are going to be using a genuine cron you may wish to disable the automatic syncing of feeds in the iCal Sync page (Settings > Event Organiser > Import/Export) since we you shall be handling this at the server level.

Creating your cron job

First ssh onto your server:

ssh yourusername@yourdomain.com

Unless you’ve set up public/private keys you will be prompted for your password. Next run the command

crontab -e

to edit your crontab file. This lists all your cron jobs with each line corresponding to one job and of the format:

minute hour day month day-of-week command-to-execute

The first 5 values indicate the frequency with which the job is run and the last value indicates the cron job to run.

The command to run is:

wp --path=/path/to/your/site eo ical-feed fetch --all

Values indicating the frequency of the job must all be specified and must take the values:

Field Range of values
minute 0-59, or *
hour 0-23, or *
day 1-31, or *
month 1-12, or *
day-of-week 0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc), or *

The values specify which minute/hour/day etc you want the job to run on. The ‘wildcard’ * means every (minute/hour/day etc).

For example

 0 2 * * * wp --path=/path/to/your/site eo ical-feed fetch --all

will fetch all feeds at 2am every morning:

You can import feeds at different times/frequencies. For example to import feed with ID 123 (you can view your feed IDs by running wp eo ical-feed list) at 2am every day and the feed 456 at 3am every Sunday:

0 2 * * * wp --path=/path/to/your/site eo ical-feed fetch 123
0 3 * * 0 wp --path=/path/to/your/site eo ical-feed fetch 456