Search Our Database

CronJob and Example

Last updated on |
by

Introduction

The cron command line is a job scheduler utility available on Unix-like operating systems such as Linux. It allows users to schedule services or tasks (known as CronJobs) to run at fixed times, periodically. These tasks can be configured to execute at intervals ranging from minutes, hours, daily, weekly, monthly, or a custom interval.

This guide provides examples of using cron to execute PHP scripts and URLs through different methods like PHP, wget, and cURL.

Prerequisites

  • Access to a Unix-like operating system such as Linux.
  • Basic knowledge of the terminal and crontab syntax.
  • Access to the crontab file (usually as a root or privileged user).
  • PHP, wget, and cURL installed on the server, depending on the method you plan to use.
  • A script or URL that you want to execute periodically.

Step-by-Step Guide

Step 1: Open the crontab editor

To begin, open your crontab editor using the following command in the terminal:

crontab -e

This will open the cron configuration file for the current user. You can add your desired cron jobs here.

Step 2: Set up the PHP script execution

To execute a PHP script every 10 minutes, add the following line to your crontab:

*/10 * * * * /usr/local/bin/php -q /home/username/domains/domain.com/public_html/pathtoscript/script.php > /dev/null 2>&1

Note: Replace username, domain.com, and pathtoscript with your actual details.

Step 3: Set up the wget script execution

If you prefer to call the script’s URL using wget every 5 minutes, add this line to your crontab:

*/5 * * * * /home/username/wget --output-document=/dev/null http://domain.com/pathtoscript/script.php > /dev/null 2>&1

Step 4: Set up the cURL script execution

To run a PHP script via its URL using cURL every 5 minutes, add the following line to your crontab:

*/5 * * * * /home/username/curl --output-document=/dev/null http://domain.com/pathtoscript/script.php > /dev/null 2>&1

Step 5: Verify your CronJob

After adding the desired lines to your crontab file, save and exit the editor. You can verify that your cron jobs are active by running:

crontab -l

This will list all active cron jobs for the current user.

 

 

Conclusion

Setting up CronJobs is a powerful way to automate tasks and script executions on Unix-like systems. With the correct cron expressions, you can ensure that your PHP scripts and URLs run at desired intervals without manual intervention. This guide covered how to execute PHP scripts using different methods: via direct PHP execution, using wget, or using cURL.

For more information on CronJob timings, refer to crontab.guru, which is an excellent tool for checking cron schedule expressions.

For additional assistance or if you encounter any issues, please contact our support team at support@ipserverone.com.