In this tutorial, we will explore the basics of cron, a server-based scheduling tool commonly used in Linux systems to automate repetitive tasks, such as update checks, session management, sitemap regeneration, inventory updates, and more. We will cover the benefits of using server-side cron, adding cron jobs, and some best practices for writing cron jobs.
Faster and More Reliable Scheduled Tasks with Server-Side Cron
While some content management systems (CMS), such as WordPress and Drupal, provide built-in cron and task management features, these solutions often hinder performance, especially for high-traffic websites. Built-in cron jobs frequently execute tasks during website visits, leading to resource contention and performance bottlenecks.
Our server-side cron offers a more reliable and efficient approach to scheduling tasks. By operating independently in WordPress, Drupal, or Magento, your cron jobs can execute tasks in the background without impacting website performance or user experience. This ensures that critical tasks are executed promptly and reliably, even for websites with a low volume of visitors.
Cron Job Best Practices
- Efficient script execution: Instead of triggering scripts via HTTP (using
wget
,curl
, etc.), execute scripts directly using PHP or the shell. This approach is more efficient and consumes fewer resources. - Minimum cron job frequency: Run cron jobs as infrequently as possible to minimise resource consumption and reduce the workload on the server and databases.
- Prevent overlapping cron jobs: If multiple cron jobs run simultaneously, ensure they don’t overlap; utilise the ‘flock’ command to introduce delays between cron jobs. For WordPress, this can easily be set up with our Servebolt Optimizer plugin.
- Thorough testing: Manually test your cron jobs to verify their functionality and ensure they operate as intended. For WordPress, the following command can be used from your webroot directory to run WordPress cron manually:
wp cron event run --all
This command forces WordPress to immediately execute all scheduled cron jobs. Additionally, running this command will display any errors that are created during the execution of your cron jobs. For more detailed information on debugging cron errors, please refer to this article.
Adding Cron Jobs in the Servebolt Admin Panel
Servebolt’s integrated cron support within our Admin Panel simplifies the process of setting up cron jobs. This eliminates the need to manually manage complex crontab files. This user-friendly interface enables you to schedule and manage your cron jobs.
If you need help figuring out the cron schedule, this resource should be very helpful.
Cron syntax is used to specify the execution time. The following example demonstrates how to run the cron.php
script in the public folder every five minutes:
*/5 * * * * /usr/bin/php ~/public/cron.php
To add a second cron job that runs at the same intervals but with a 30-second delay, use the following command:
*/5 * * * * sleep 30 && flock -n /tmp/cron_job_2.lock /usr/bin/php ~/public/cron_job_2.php
Paths to Common Commands
The following are the paths to some of the most common commands used in cron jobs:
/usr/bin/php
: The PHP interpreter/usr/bin/sh
: The Bourne-again shell (Bash)/usr/bin/wget
: The GNU wget downloader
Should you have any additional questions please don’t hesitate to contact our support chat at servebolt.com!