HomeBlogTech UpdatesCrontab syntax basics, and how it differs from WP-Cron
Tech UpdatesSeptember 5, 20263 min

Crontab syntax basics, and how it differs from WP-Cron

Crontab Syntax Basics, and How It Differs from WP-Cron When you set up task scheduling on a server, you've likely encountered the expression `*/5 * * * *`. But...

Crontab Syntax Basics, and How It Differs from WP-Cron

When you set up task scheduling on a server, you've likely encountered the expression */5 * * * *. But what does it actually mean? In this article, we'll explore the basic syntax of crontab and also delve into its differences from WP-Cron in WordPress.

What is crontab?

Crontab is a job scheduling utility that comes built into all Unix-like systems (including Linux and macOS). The name comes from the words cron table, meaning a configuration file and command to manage it. This file specifies what needs to be run and when, line by line.

Basic Fields in crontab

Each line in crontab consists of five fields, followed by a command to execute.

  1. Minutes: Range 0-59.
  2. Hours: Range 0-23 (24-hour format).
  3. Day of the Month: Range 1-31.
  4. Month: Range 1-12.
  5. Day of the Week: Range 0-7 (0 and 7 represent Sunday).

Examples of Field Usage

* * * * * command

This line runs the command every minute.

0 0 * * * command

This line runs the command at midnight every day.

15 10 * * * command

This line runs the command at 10:15 AM every day.

*/5 * * * * command

This line runs the command every 5 minutes.

Difference Between crontab and WP-Cron

WP-Cron in WordPress uses its own scheduling system, which can be less precise compared to crontab. In WP-Cron, tasks are executed either when a user visits the site or automatically after a certain period if no one is visiting the site.

Advantages of Using crontab

  1. Precision: Tasks are executed strictly at the specified time.
  2. Resource Management: You can more precisely control server resource usage.
  3. Independence from Site Visits: Tasks run independently of whether users visit the site.

Switching from WP-Cron to crontab

Before switching to crontab, ensure that all tasks you plan to run can be executed without user interaction.

Steps for Switching

  1. Create a .env File:

    WP_CRON_LOCK_DIR=/path/to/your/cron_lock
    
  2. Use Plugins for Automation: There are several plugins like Better WP Cron that help automate the process.

  3. Configure crontab:

    crontab -e
    

    Add lines to run the tasks:

    * * * * * /usr/bin/php /path/to/your/site/wp-cron.php
    

Practical Tips

  1. Check Documentation: Ensure you fully understand the crontab syntax.
  2. Use @reboot: For tasks that should run on system boot:
    @reboot /usr/bin/php /path/to/your/site/wp-cron.php
    
  3. Testing: Before applying changes to a production server, test them on a staging environment.

Conclusion

Understanding the crontab syntax will help you effectively manage tasks on your server and ensure more precise task execution. While WP-Cron is convenient for simple tasks, crontab offers more control and precision.