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
- Create a custom directory at
~/www/site.url/user-scripts/
for all custom scripts and a subdirectory for/logs/
.
- 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
- 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.
- 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):
- Edit the crontab at root:
*/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
- 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"
},
- 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
- 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
- Make the file executable with this:
chmod +x /var/www/site.url/user-scripts/delete-logs-script.sh
- 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