Check WordPress wp-cron Count with WP-CLI + Monit + Email Alerts

Monit is a very flexible Montiroing tool to help ensure your services are always running. It can be used to send email alerts (guide with Mailgun) when processes crash and automatically restart them. On all the VPS and dedicated servers I set up for my clients on Codeable, I configure Monit for proactive monitoring.

wp-cron can sometimes stall and have thousands of latent cronjobs that haven’t run. Instead of monitoring this manually, this tutorial will show you how to use bash and WP-CLI to count how many cronjobs are in the wp-cron queue. Taking it one step further we will configure Monit to use this bash script and alert you by email if the number of cronjobs in queue exceed a certain number that you specify. This post is based on how to monitor WordPress folder size with Monit.

Check WordPress wp-cron Count with Monit + Email Alert

This WP-CLI command will list the cron events

wp cron event list

To get the count we can remove the column headers line from the table with sed and then count the lines with wc

wp cron event list --allow-root --skip-plugins --skip-themes | sed -n '1!p' | wc -l

Create the script

mkdir ~/scripts
nano ~/scripts/

Paste the script which takes the WordPress path as the first argument and number of cronjobs to alert if exceeded as the second argument

# Author Mike from

#capture first passed variable

#capture second passed variable

#get cronjob count
WPCRONCOUNT=$(wp cron event list --path=$WPPATH --allow-root --skip-plugins --skip-themes | sed -n '1!p' | wc -l)

#output cronjobs so Monit can capture it
echo "$WP_PATH has $WPCRONCOUNT cronjobs"

#provide status code for alert
if [[ $WPCRONCOUNT -gt $(( $CRONTHRESHOLD )) ]]; then
    exit 1

Make the script executable

chmod +x

Test running the script by passing your path to the WordPress installation folder as the first parameter and the number of cronjobs that, if exceeded, should trigger an alert

bash /var/www/ 50

You will get this output

/var/www/ has 12 cronjobs

Now we can configure Monit to use this bash script and display its output

Configure Monit to Check wp-cron

Assuming you have a modular system for creating Monit configurations as outlined in the Monit installation guide, create the new snippet

sudo nano /etc/monit/conf.d/wpcroncheck

If the WordPress installation in /var/www/ has cronjobs in queue  greater than 50 we will get an email alert.

Then every 1440 cycles line means Monit will check every 1440x the Monit interval defined in /etc/monit/monitrc

check program wp-cron
    with path "/root/scripts/ /var/www/ 50"
    every 1440 cycles
    if status != 0 then alert

Ctrl+X, Y and Enter to Save and Exit.

Test Monit syntax is valid with our new addition of disk space checking and alerting.

sudo monit -t

Reload Monit if there were no errors

sudo service monit reload

Check in Monit and you can see the Program wp-cron running 🙂


wp cron Command
Remove first line from bash output