How to List and Manage WP-Cron Events Using WordPress CLI?

Updated on September 23, 2021

What is Cron?

Cron is one of the most useful utilities that you can find in any Unix-like operating system. It is a Linux command used for scheduling tasks to be executed sometime in the future without any user-based action. “Cron Jobs” refers to these pre-programmed tasks or commands. Cron is commonly used to schedule backups, monitor disk space, regularly remove “no longer needed files” such as log files, do system maintenance activities, and much more.

Now that we’ve become familiar with cron, it will become very easy for us to understand what WP-Cron is going to be.

What is WP-Cron?

In WordPress, automated tasks are handled by WP-Cron, which is used to simulate a system cron jobs. Some standard examples of a WordPress cron job might involve planning a post to publish, checking for updates, deleting old comments from trash, or checking for plugin or themes updates.

Note: Irresponsible use of WordPress cron by plugins can also slow down the website performance. Especially if you are on shared hosting.

WP-CLI is a wonderful tool to have if you need to make adjustments to scheduled events on your WordPress site. We’ll teach you how to use WP-CLI to list, run, create, and delete events within the WordPress cron.

Note: It is recommended that you manage the WordPress cron through WP-CLI then disable the WP-Cron from the wp-config file. For further instruction on how to disable WP-Cron, kindly visit our knowledge base article.

1) Connect SSH:

To start working on the Cron Jobs, first, you need to connect your application vis SSH and SFTP.

Once the SSH connection is established, go to the public_html folder by running the following command:

cd applications/application_folder_name/public_html/

2) List All Cron Events:

To list out all cron events which are presently scheduled on the WordPress site, you will need to execute the following command:

wp cron event list

You can also add more parameters to list cron events based on your needs. For instance, if you would like to list the scheduled WP-Cron events in JSON format, extra two fields are also required. Which you can see below:

wp cron event list --fields=hook,recurrence --format=json

You can select the format that best meets your requirements; however, it will appear in the table by default if the format is not specified on the command.

3) Execute All WP-Cron Events:

You may use the following command to perform all remaining cron events (events that are due but haven’t been executed yet):

wp cron event run --due-now

4) Manually run all scheduled WP-Cron:

You may use the following command to manually perform all WordPress scheduled events, regardless of whether they are due or not:

wp cron event run –all

5) Create a new cron event:

WP-CLI may also be used to plan new WP-Cron events. Any action hook on your site can have new cron events added to it. The following techniques are available for setting up new cron events.

  • Schedule New Event for the Next Cron Run:

The command below will create a new cron event for the specified action hook. This event will be triggered the next time the cron is executed. The command is as follows:

wp cron event schedule example_action

Note: Make sure to replace example_action with the name of the action hook that you want to execute.

  • Schedule New Event for a Specific Date/Time:

Setting up the WordPress cron event to execute on the required date and time is similar to the previous example but needs an extra parameter that includes the date and time that the event should run.

wp cron event schedule example_action '+1 hour'

As seen above, a new event will be scheduled to run example_action in 1 hour.

You can also set the cron which are giving below because the following function PHP strtotime() function convert any string to the timestamp, which is valid for the cron execution.

Example #1: Schedule the Event on a Specific Date

wp cron event schedule example_action '7 July 2019'

Example #2: Schedule the Event in 1 Week

wp cron event schedule my_example_action '+1 week'

Example #3: Run the Event Next Monday

wp cron event schedule my_example_action 'next Monday'
  • Schedule a New Recurring Cron Event:

To establish a new WordPress cron event that will recur after a certain period, add another parameter indicating how frequently the event will be executed. For example, the following cron will be run once daily:

wp cron event schedule example_action now daily

The possible intervals are dependent on any cron intervals that are currently set. WordPress includes the following interval names by default:

  • Hourly: Runs the event once per hour.
  • Twice daily: Runs the event twice every day (every 12 hours).
  • Daily: Runs the event once every day (every 24 hours).

6) Deleting a scheduled cron event:

Deleting a scheduled cron event with WP-CLI is quite straightforward. The command that needs to be executed is given below:

wp cron event delete example_action

Conclusion:

That’s all! We hope this knowledge base article was helpful in how to see and handle WordPress cron tasks. If you are more interested in learning about WP-CLI’s different features, please follow this link: https://developer.wordpress.org/cli/commands/.

Need More Help!

Type in your question at the knowledge base website or comment below

Leave a Comment

Your email address will not be published. Required fields are marked *