Skip to main content

Setting up Cron on Multisite

Disable wp-cron and enable gp-cron for main site

gp site {site.url} -gpcron-on {minute.interval}

Example:

gp site digitalchurch.app -gpcron-on 60

Setup Script & Master Cron Job

  1. Create a custom directory at ~/www/site.url/user-scripts/ for all custom scripts and a subdirectory for /logs/.

CleanShot 2023-05-01 at 12.13.37@2x.jpeg

  1. In that directory, create a script that will loop through all sites called multisite-cron-script.sh. No editing of this file is necessary.
#!/bin/bash
default_sleep=1
multi_site="$1"
sleep_for_seconds="${2:-$default_sleep}"
sys_user=$(/usr/local/bin/gp conf read sys-user -site.env "${multi_site}" -q)
for site in $(sudo su - "${sys_user}" -c "/usr/local/bin/wp site list --field=url --path=/var/www/${multi_site}/htdocs"); do
sudo su - "${sys_user}" -c "/usr/local/bin/wp cron event run --due-now --url=${site} --path=/var/www/${multi_site}/htdocs"
sleep ${sleep_for_seconds}
done
  1. On the command line, make that script executable by running the following command:
chmod +x /var/www/site.url/user-scripts/multisite-cron-script.sh
note

The script accepts two args, for {primary-domain:string} and {seconds:integer}. You'll send the primary domain and the rhythm of the cronjob in seconds from the cronjob.

  1. Create a cronjob in the crontab (Use the root crontab, not the systemuser).
    • Edit the crontab at root: crontab -e
    • At the bottom, add the following (don't forget to swap out the site.url with the site domain):
*/10 * * * * /var/www/site.url/user-scripts/multisite-cron-script.sh site.url 2 1>> /var/www/site.url/user-scripts/logs/multisite-cron.log 2>> /var/www/site.url/user-scripts/logs/multisite-cron-errors.log
  1. The cronjob will write to two log files. To watch these files in vscode log watcher, add these to the log watcher settings:
{
"title": "Custom Multisite Cron",
"pattern": "/var/www/site.url/user-scripts/logs/multisite-cron.log"
},
{
"title": "Custom Multisite Cron Errors",
"pattern": "/var/www/site.url/user-scripts/logs/multisite-cron-errors.log"
},
  1. Make sure you test the cron jobs and know that the logs are outputting correctly. Once that is done, you can setup a monthly log deletion script below to save space:

Setup Log Deletion

  1. Add this delete-logs-script.sh file to the user-scripts directory:
#!/bin/bash
rm /var/www/site.url/user-scripts/logs/multisite-cron.log /var/www/site.url/user-scripts/multisite-cron-errors.log
  1. Make the file executable with this:
chmod +x /var/www/site.url/user-scripts/delete-logs-script.sh
  1. Add a cronjob to run the removal script on the first of every month:
0 0 1 * * /var/www/site.url/user-scripts/delete-logs-script.sh > /dev/null 2>&1

Resources:

GridPane Community - A GridPane Community Forum

Delete thousands of cron jobs