I AM LISTENING TO
|
WAS ICH LIEBE
  • Englisch
  • Deutsch

WordPress Cron + WP-CLI + Ntfy

19. Februar 2024




WordPress Cron + WP-CLI + Ntfy

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




Abonnieren
Benachrichtige mich bei
guest
0 Comments
Inline Feedbacks
View all comments



Alexander

Ich bin ein Full-Stack-Entwickler. Meine Expertise umfasst:

  • Server-, Netzwerk- und Hosting-Umgebungen
  • Datenmodellierung / Import / Export
  • Geschäftslogik
  • API Layer / Action Layer / MVC
  • Benutzeroberflächen
  • User Experience
  • Verständnis für Kundenwünsche und Geschäftsbedürfnisse

 

Ich liebe die Entwicklung, das Design und kenne mich auch mit Serverarchitekturen aus.  Ich würde mich nie vollständig fühlen, wenn einer der Bereiche fehlen würde.

Ich habe ein breites Interessengebiet, deshalb tauche ich ständig in neue Technologien ein und erweitere mein Wissen, wo immer es nötig ist. Die Technologien entwickeln sich schnell und ich genieße es, die neuesten Technologien zu nutzen.

Abgesehen davon bin ich ein friedliebender Kerl, der versucht, Leute um sich herum zu haben, die dasselbe denken.  Ich glaube wirklich an das Prinzip: "Wenn man jemandem hilft, wird einem jemand helfen, wenn man es braucht."

Previous Next