CHECKING STATUS
I AM LISTENING TO
|

WordPress Cron + WP-CLI + Ntfy

4. January 2025
.SHARE

Table of Contents

THE GOAL

Create a system cron for WordPress, that is accessible and can be easily tweaked to provide more details. Here some basic information about crons and the tools I am going to use …

WordPress CRON

In WordPress, the term “cron” refers to the system used for scheduling tasks to be executed at predefined intervals. The WordPress cron system allows various actions to be scheduled, such as publishing scheduled posts, checking for updates, sending email notifications, and running other scheduled tasks.

WordPress includes its own pseudo-cron system, which relies on visitors accessing your site. When a visitor loads a page on your WordPress site, WordPress checks if there are any scheduled tasks that need to be executed. If there are, it runs those tasks. This system works well for most sites, but it has limitations, particularly for low-traffic sites or sites that need precise scheduling.

To overcome these limitations, WordPress also provides the option to use a real cron system. With a real cron system, tasks are scheduled and executed independently of visitor traffic. This can be more reliable and precise than relying on visitors to trigger cron tasks.

To set up a real cron system for WordPress, you typically need to configure your server’s cron job scheduler to trigger the wp-cron.php file at regular intervals. This file handles the execution of scheduled WordPress tasks.

List scheduled WordPress events

WP Crontrol is a solid UI, to list and see whats happening in the background.

  • View all cron events along with their arguments, recurrence, callback functions, and when they are next due.
  • Edit, delete, pause, resume, and immediately run cron events.
  • Add new cron events.
  • Bulk delete cron events.
  • Add and remove custom cron schedules.
  • Export and download cron event lists as a CSV file.

WP-CLI

WP-CLI (WordPress Command Line Interface) is a powerful command-line tool that allows developers and administrators to interact with WordPress websites directly through the command line, without needing to use a web browser.

It provides a wide range of commands for managing various aspects of a WordPress site, such as executing crons / scheduled tasks, installing plugins, updating themes, managing users, and much more.

Most WordPress hosts have it preinstalled. Installation

Test CRON

Run CRON

ntfy.sh

ntfy (pronounced notify) is a simple HTTP-based pub-sub notification service. It allows you to send notifications to your phone or desktop via scripts from any computer, and/or using a REST API.

You can host your own docker instance or use the hosted solution.

The documentation is detailed and offers many ways to tweak the resulting notification.

Server CRON / THE OLD WAY

Server – CRON / THE NEW COMBINED WAY

I am not showing you how to create a system cron, that can vary depending on your hosting provider. Some of you will just setup / modify the crontab themselves. So here an example of how I use it these days …

Lets dissect this :)

Execution Timeframe – Minute / Hour / Day (month) / Month / Day (week)

See example usage on crontab.guru.

Capture the output

Im using a variable to capture the output, allowing me to pass it to ntfy.

The command part 1

Use bash to launch wp-cli, passing in required parameters to make sure the right website is targeted.
I am using –due-now to only launch those schedules that are actually pending. >/dev/null 2>&1 prevents any emails to be send for this cron job, it redirects the error stream into the output stream. Always helpful to remove it for the first testdrive.

>/dev/null: redirects standard output (stdout) to /dev/null
2>&1: redirects standard error (2) to standard output (1), which then discards it as well since standard output has already been redirected :)

The command part 2

This part sends the output to a ntfy instance / topic.

  1. -u %token% login / auth with your token
  2. -H “Filename: …” this will move the body text into an attachment file. You can remove this and all output will be part of the body text. ntfy has a body message length limit and will shift to an attachment when reached.
  3. -H “Title: …” Title for the message
  4. -d “$Output” Set the body message to the output we captured, using the set variable
  5. Finally the url to post to, the ntfy instance and topic.

You can set many other things as well, like tags, images …. Check the documentation about publishing for more options. You can even redirect to an email account ;)

Detecting WP-CLI execution programmatically

Detecting WP-CRON execution programmatically

Enjoy coding …
Alex

Let’s Talk!

Looking for a reliable partner to bring your project to the next level? Whether it’s development, design, security, or ongoing support—I’d love to chat and see how I can help.

Get in touch,
and let’s create something amazing together!

RELATED POSTS

Days Of Docker 6

Day 6: Monocker – Monitor Docker – 7 Days of Docker

Monitoring the health and status of Docker containers is crucial for maintaining a reliable and efficient environment. Monocker is a lightweight tool designed to address this need by monitoring Docker container state changes and sending real-time alerts through various messaging platforms. What is Monocker? Monocker, short for “MONitors dOCKER,” is an open-source application that keeps […]

The Letters Of The Alphabet Are Arranged Crosswise 2023 11 27 05 02 11 Utc

Best Solutions to Run WordPress Locally 2025

Running WordPress locally is essential for testing, development, and experimentation without affecting a live website. There are several solutions available, each catering to different needs. Below, we explore the best ways to set up WordPress on your local machine, along with useful links. 1. Local by Flywheel Local is a powerful, free local development environment […]

Days Of Docker5

Day 5: mosparo an Open-Source Spam Protection solution – 7 Days of Docker

If you’re looking for an efficient and streamlined way to integrate mosparo, an open-source spam protection solution, into your workflow, running it via Docker is an excellent choice. Docker allows you to deploy mosparo quickly without worrying about dependencies or manual configurations. In this guide, we’ll walk through setting up mosparo with Docker. Prerequisites Before getting […]

Alexander

I am a full-stack developer. My expertise include:

  • Server, Network and Hosting Environments
  • Data Modeling / Import / Export
  • Business Logic
  • API Layer / Action layer / MVC
  • User Interfaces
  • User Experience
  • Understand what the customer and the business needs


I have a deep passion for programming, design, and server architecture—each of these fuels my creativity, and I wouldn’t feel complete without them.

With a broad range of interests, I’m always exploring new technologies and expanding my knowledge wherever needed. The tech world evolves rapidly, and I love staying ahead by embracing the latest innovations.

Beyond technology, I value peace and surround myself with like-minded individuals.

I firmly believe in the principle: Help others, and help will find its way back to you when you need it.