Disable wp-cron
Still, we need to disable WordPress default wp-cron behaviour by adding following line to wp-config.php file:

Copy to Clipboard

This will stop the internal WP-Cron function from executing on every page load.

Please note:You are not disabling the WP Cron itself. You just disabling it’s execution by WordPress on every pageload and replacing it with execution by regular cronjob. cronjob will execute all WP Cron tasks.

Set Up a Linux Cron

To create or edit a crontab file run the following command:

Copy to Clipboard

Note: without –delete-after option, wget would save the output of the HTTP GET request.
A daily call to your site’s WordPress Cron that triggers at midnight every night could look similar to:

Copy to Clipboard

2分钟一次

Copy to Clipboard

cURL (Preferable)未测试

Copy to Clipboard

OR
wget

Copy to Clipboard

如何判断定时任务正常执行?
1.首先确认crontab有正常调用你的命令
查看下cron的日志: tail /var/log/cron,看到类似如下的日志说明crontab有在正常调用命令。

Copy to Clipboard

显示情况

Copy to Clipboard

List Existing Cron Jobs
You can list all cron jobs on your system without opening the crontab configuration file. Type in the following command in a terminal window:

Copy to Clipboard

Basic Crontab Syntax
Cron reads the configuration files for a list of commands to execute. The daemon uses a specific syntax to interpret the lines in the crontab configuration tables.

To be able to set up a cron job, we need to understand the basic elements that make up this syntax. The standard form for a crontab line is as follows:

a b c d e /directory/command output
So, the parts of a cron command are:

1. The first five fields a b c d e specify the time/date and recurrence of the job.

2. In the second section, the /directory/command specifies the location and script you want to run.

3. The final segment output is optional. It defines how the system notifies the user of the job completion.

1. Cron Job Time Format
The first five fields in the command represent numbers that define when and how often the command runs. A space separates each position, which represents a specific value.

The table below summarizes possible values for the fields and the example syntax:

Field Possible Values Syntax Description
[a] – Minute 0 – 59 7 * * * *  The cron job is initiated every time the system clock shows 7 in the minute’s position.
[b] – Hour 0 – 23 0 7 * * * The cron job runs any time the system clock shows 7am (7pm would be coded as 19).
[c] – Day 0 – 31 0 0 7 * *  The day of the month is 7 which means that the job runs every 7th day of the month.
[d] – Month 0 = none and 12 = December 0 0 0 7 * The numerical month is 7 which determines that the job runs only in July.
[e] – Day of the Week 0 = Sunday and 7 = Sunday 0 0 * * 7  7 in the current position means that the job would only run on Sundays.