Skip to content

How to Schedule a Cron Job for PHP: A Step-by-Step Guide

Steps
4
Category
PHP
Covers
Cron
Versions & support
About PHP →
On this page 4 steps

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 of 4Understand the Basics of Cron Syntax

Before setting up a Cron job, it is important to understand the basic syntax of a Cron entry:

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 of 4Write 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:

Step 3 of 4Schedule 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:

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 of 4Confirm 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.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top