Run PHP script automatically at a specified time using Cron Jobs

In this article, I am going to explain How to Run PHP script automatically at a specified time using Cron Jobs.

If you are a website developer and working on web related projects, sooner or later you will face  the situation, when you need to automate the code execution task on server.

Automation task like  sending email to clients, cleaning junk data from sever or backing up the database & files on server.

You can definitely code script in PHP to perform such function on server. Whenever anybody visit that page, it  gets executed and you will get the result.

But what if you want the script to be executed only at a specified time like week on the month, daily or hourly?

To handle such scenario on server, every server manager provide a software to execute the script by scheduling.

If you are using Unix/Linux based hosting it comes with Cron utility to automate tasks.

Now what is Cron Job?
Answer is: Cron is a Linux utility which schedules a command or script on your server to run automatically at a specified time and date, and Cron Job is the scheduled task itself.

Next question is How you Run PHP script automatically at a specified time using Cron Jobs?

Run PHP script automatically at a specified time using Cron Jobs?

To execute your script automatically on server let’s start the process by Login to cPanel.

  1. After successful login to cPanel, look for Advance Tab on cPanel dashboard and click Cron Jobs link.
    cron jobs
  2. It will redirect you to cron job setting page similar to below image.
    create cron job
  3. Now click on — Common Settings — Option, it will show you all the possible timing to execute your script.
  4. Let’s suppose you want to execute your script at the end of the day, daily at 11:59 PM.
  5. To achieve this select Option — Once Per Day
  6. Selecting this option will populate the text boxes below the dropdown.
  7. Now in minute text box enter the value 59, because we want our script to be executed at 59 minutes of an hour.
  8. Now in next text box, enter the hour which will be 23 as per 24 hours clock.
  9. Leave all text boxes as it is with * , which mean every month or weekday.
  10. Now in command text box, you need to enter the php location and path of  file to execute.
    In cPanel, path of php vary from server to server, you can use any of the below php path for execution.
    /usr/local/bin/php
    /usr/bin/php
    In my case it’s /usr/bin/php.
  11. Now to execute your script by cron you have to find it’s file path on server which will look like “/home/your_username/public_html/path_of+php_file.php
    Run PHP script automatically at a specified time using Cron Jobs
  12. So you command text box now look like /usr/bin/php -q /home/your_username/public_html/path_of+php_file.php where -q option is for “quiet” — it forces PHP to not print the headers.
  13. After completing these step, press the “Add New Cron Job” button below and we are done creating the cron job.

Wait for 23:59 in your clock to see the result.
If you have any question or query regarding the Cron Job, you can ask it on our Ask Question section of website.

Watch the video for demonstration to Run PHP script automatically at a specified time using Cron Jobs.