Categories
wp-cli wp-cron

Setting Up Reliable WordPress Cron

We all know wp-cron can be unreliable if left on default WordPress setting, WordPress’s behaviour on maybe I will run maybe not for wp-cron in a case of sites with less traffic.

There are following solutions to solve this problem. All require to disable default WP cron.

This will work for both single and multisite WordPress installation.

  1. Linux Crontab
  2. For Scalable and time-sensitive cron jobs.
  3. Sites like Pingdom and Uptimebutler (Not covered)

1. Linux Crontab

https://gist.github.com/PatelUtkarsh/3c50655d3606943b5801652a6aaa5634

This script will work on both WordPress multisite and single site.

WP_PATH="/path/to/wp"

Make the script executable:

chmod +x execute-wp-cron.sh

Test it by running ./execute-wp-cron.sh If it does not output anything go to next step else fall back.

Every user has their own crontab so I’ll recommend editing your Nginx / Apache user’s crontab ( www-data or http )

To edit run:

crontab -e -u {user-name}

And add the following line and update your script path

* * * * * /some/folder/execute-wp-cron.sh

This will tell Linux to check for wp-cron every minute and if there is any new cron yet to execute in a pipeline it will execute it.

You can update cron time according to your preference, This site https://crontab.guru may help to understand parameters.

2. Scalable and time-sensitive cron jobs.

If you want a highly scalable cron then I strongly recommend checking the following repo:

  1. https://github.com/humanmade/Cavalcade 
  2. https://github.com/Automattic/Cron-Control

☝️It’s recommended for sites with high traffic which is relying on cron and running time-critical cron.

Both require different setup you can find that on their github/product page and pick one which you feel works for you.

Disable default cron

Lastly, disable wp-cron by adding this line in wp-config.php

define( 'DISABLE_WP_CRON', true );
// Mind space WPCS.

You can test if cron is running by wp cron event list

And that’s it. 🎉

Categories
wp-cli

Backup WordPress site using Dropbox and wp-cli ?

Here is how you can backup small site using wp-cli and dropbox.

Go to: https://www.dropbox.com/developers/apps

Create new app call it anything it’s gonna be your private app.

Generated access token and save it somewhere in your locker 🙂

Download file backup-site.sh and change directories path and DROPBOX_TOKEN

sudo chmod +x backup-site.sh

Setup crontab with crontab -e add following line there

0 5 * * * /root/backup-sites.sh > /dev/null 2>&1

This will backup site everyday at 5 am.

Notes:

  • This scripts assumes your installation will be under example.com/htdocs if not it’s time to fork this and modify script.
  • This will backup your WP tables and wp-content folder.
  • You can also change path of backup-sites.sh.
  • You should clean up your dropbox folder before you run out of memory.
  • Modify crontab if you want to change backup interval to less frequent or more frequent.

Credit: