Cron is a task scheduler in Unix-based operating systems. It is used to schedule commands or scripts to run periodically at fixed times, dates, or intervals. This feature can be particularly useful for automating system maintenance or administration tasks.
One common use case for Cron is to automate scripts written in PHP, a popular server-side scripting language. This article will walk you through how to schedule a Cron job for a PHP script, providing practical examples for better understanding.
Step 1: Understand the Basics of Cron Syntax
Before setting up a Cron job, it is important to understand the basic syntax of a Cron entry:
1 2 3 4 5 6 7 8 | * * * * * command to be executed - - - - - | | | | | | | | | +----- day of the week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of the month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59) |
Each field (represented by an asterisk) can have a single value, multiple values separated by commas (for example, “1,2,3”), a range of values (for example, “1-3”), or an asterisk to indicate “every”.
Step 2: Write Your PHP Script
In order to schedule a Cron job, you first need a PHP script to schedule. As a simple example, let’s consider a PHP script that logs the current date and time to a text file.
Create a PHP file (cron_example.php) in a suitable directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $today = date("Y-m-d H:i:s"); $file = '/path/to/your/logfile.txt'; // Check if file exists if(file_exists($file)) { $current = file_get_contents($file); } else { $current = ''; } $current .= "Current date and time: " . $today . "\n"; file_put_contents($file, $current); ?> |
Step 3: Schedule the PHP Script as a Cron Job
To schedule the PHP script you’ve just written as a Cron job, you’ll need to access the Crontab, which is the list of scheduled tasks for the Cron daemon.
In the terminal, enter the following command:
crontab -e
This command opens the current user’s Crontab in the default text editor.
Let’s say you want to run the script every day at 3:30 AM. The Cron syntax would look like this:
1 | 30 3 * * * /usr/bin/php /path/to/your/cron_example.php |
In this line:
- “30 3 * * *” means “at 3:30 AM every day”.
- “/usr/bin/php” is the path to the PHP executable. You may need to adjust this depending on your server configuration.
- “/path/to/your/cron_example.php” is the full path to the PHP script you want to run.
Simply add this line to the end of the Crontab file, save it, and exit the text editor.
Step 4: Confirm Your Cron Job is Scheduled
To ensure your Cron job has been scheduled correctly, you can list all scheduled Cron jobs for the current user by entering the following command in the terminal:
crontab -l
You should see the line you just added to the Crontab.
Conclusion
This article provides a brief introduction on how to schedule a Cron job for a PHP script, with an example of a script that logs the current date and time to a text file. As you can see, Cron is a powerful tool for automating tasks on a Unix-based system, and PHP scripts can be scheduled just like any other command. With a good understanding of Cron syntax and the steps provided above, you should now be able to schedule your own PHP scripts as Cron jobs.