How To Set Up Infinite WP’s Cron Job


If you’ve never set up a cron job before it can be a little daunting to do so when asked.

As I hadn’t done a cron job for quite some time I had to remember all the processes involved in getting a task set up and being able to check the task was working. On my Ubuntu 13.10 nginx box I went through the following steps and hopefully it helps clear the confusion on how to set it up on your server.

After installing the necessary stuff according to InfiniteWP one of the final tasks will be enabling a cron task on your server. I’m assuming that you have access to your server so once you’ve logged into it, navigate to:

$ cd /etc

Once here, look at the directory by running the ls command and see if you spot the following files and directories:

cron.d
cron.daily
cron.hourly
cron.monthly
crontab

System’s PHP check

Now before we get to adding stuff let’s make sure your server CAN run PHP on the command line. For this run the following:

$ php -v

If you receive an error that apt-get cannot find or will need to install php, then you need to install the php5-cli application, hence run:

$ sudo apt-get update
$ sudo apt-get install php5-cli

Once you’ve checked to make sure you can run php commands from the command prompt you can then safely move to the next step.

InfiniteWP cron setup

To get InfiniteWP to work you will need to navigate within the cron.d folder and to create a file that you will be able to identify for InfiniteWP’s cron tasks for your WordPress installs. I’m going to create a file called iwp, but you can call it whatever you want.

$ nano iwp

Within this file append the necessary information to create a cron task:

# for InfiniteWP's cron
*/20 * * * * root php -q -d safe_mode=Off /directory/to/wordpress/iwp/cron.php >/dev/null 2>&1

Be sure to change the /directory/to/wordpress/iwp/cron.php to whatever the full path location is to your InfiniteWP’s install is.

But what does all this stuff mean?

  • The first segment */20 means that the following task will run at every 20 minute interval.
  • The next segment * means that it will run every hour.
  • The next * * * (three asterisks) mean every day of the month, every month, every day of the week. Obviously if you wanted to change the frequency of how often you want the cron task to run you would modify these variables. I’d highly encourage you to read more about cron jobs by going to the manual here.
  • After all the asterisks we then set who will be the user running this task, in this case I’m setting it as root, followed by the command I’d like to be able to run.
  • In the case of InfiniteWP we run a php command in quiet mode with the following detail of keeping safe_mode off running the php file found at the location of /directory/to/wordpress/iwp/cron.php putting all stdout output into the garbage bin.

Once you’ve saved your iwp file you’ll want to reload cron (just for safe-keeping, even though cron should be able to grab the new commands) by running:

$ sudo service cron reload

If you wanted to make sure cron indeed is running and that it is working you could similarly run:

$ sudo service cron restart

Which should output something like:

cron stop/waiting
cron start/running, process 15910

The final check all is working correctly on our end is to check the /var/log/syslog file to see that it has worked. To do this, possibly wait for 20 minutes (or however long you set your cron job to run for) and run this:

$ grep CRON /var/log/syslog

This should output something like this:

Jun 24 01:20:01 SERVER_NAME CRON[7772]: (root) CMD (  php -q -d safe_mode=Off /directory/to/wordpress/iwp/cron.php >/dev/null 2>&1)

This output shows you the timestamp of when the process ran and who ran it.

Conclusion

In this post you have learnt how to create a cron task for running InfiniteWP and how to check your cron tasks are working.

Ryan

Author of scripteverything.com, Ryan has been dabbling in code since the late '90s when he cut his teeth by exploring VBA in Excel when trying to do something more. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP. When he is not behind a screen, Ryan enjoys a good bush walk with the family during the cooler months, and going with them to the beach during the warmer months.

Recent Posts