Cron is a time-based job scheduler in Unix-like operating systems. It allows you to schedule repetitive tasks, such as running a PHP script, to be executed at specified times or intervals.
To create a cron job with PHP, follow these steps:
1. Open your terminal or command prompt and type `crontab -e` to open the crontab file for the current user.
2. In the crontab file, add a line for your PHP script. The format of a cron job entry is as follows:
“`
* * * * * command_to_be_executed
“`
The five asterisks represent the schedule for the task. You can specify the minute, hour, day of the month, month, and day of the week in each field.
For example, if you want to run your PHP script every day at 3:00 PM, you can add the following line to your crontab file:
“`
0 15 * * * /usr/bin/php /path/to/your/script.php
“`
Replace `/usr/bin/php` with the path to your PHP executable and `/path/to/your/script.php` with the path to your PHP script.
3. Save the crontab file and exit the text editor.
Your PHP script will now run automatically at the specified schedule. You can use cron to schedule tasks such as generating reports, sending emails, or updating database records.
Note: Make sure your script has the proper permissions to be executed by cron. You can use the `chmod` command to set the executable permissions for your script:
“`
chmod +x /path/to/your/script.php
“`
Also, ensure that the PHP executable is in the system’s PATH variable or provide the full path to the PHP executable in the crontab file.