How to run a php file(script) automatically in a certain interval - Using Cron job

Posted on January 18, 2008 
Filed Under php, tips and technique, tutorial

In your project you might have to run a function or script (.php) file automatically for a various reasons like automatic sending email on birthday or automatically close a auction when the end_time is reached. What to do in that situation? .To do these things automatically, you must have a php file which must contains the code and these files should be called automatically at certain certain time. So how to do that? The answer is Cron Job. Cron job are the cron tab command found in the UNIX or Linux system.

Now let’s see how can we call the file automatically at certain duration. Let’s suppose the duration is one hour, now I’ll show you how to setup the the cron-job of every one hour to run a PHP file automatically in the Cpanel of your website.

  1. Go to the Cpanel of your website.
  2. Click on cron-tab.
  3. Now there are two options Standard and Advance(Unix Style).
  4. Click on the Standard - Now select minutes to “0″ , Hours to every hour, month, days , weekdays to every option.
  5. And in the command to run text box enter “lynx -source http://your-domain/your-file.php > /dev/null“. As you can see the command called lynx which is a browsing program in Unix/Linux system and call the specified php file in every hour.
  6. To setup Advance(Unix Style), just click on that button and place 0 on minute and * on the hour, day , month and weekday and in the command just place “lynx -source http://your-domain/your-file.php > /dev/null“. The “*” means that every i.e. “*” in hour means that every hour, the same in day means that every day etc.


In the other control panel like Plesk have different environment where you can enter the the following command directly

0 * * * * lynx -source http://your-domain/your-file.php > /dev/null

Where 0 represents every 0th minute, the next “*” means every hour, the next “*” means every day, the next represent every month and the last one represent every weekday.

Popularity: 12% [?]

Enter your email address and get recent tutorials, tips, tricks and scripts of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Follow me on twitter at http://twitter.com/roshanbh.

Related Posts

» How to call php from ajax in every second using Jquery
» Hide .php extension with url rewriting using .htaccess
» Solving ‘Warning: Cannot add header information’ in PHP
» Uploading large(big) files in PHP using .htaccess

Comments

3 Responses to “How to run a php file(script) automatically in a certain interval - Using Cron job”

  1. Shakeel Shretha on March 11th, 2008 8:18 am

    My cron job didn’t run with that command. Then I used some other command like /usr/bin/wget -o- /home/……………../filename.php

    Does the command differ according to server?

  2. Roshan on March 11th, 2008 8:36 am

    well in some server lynx command might not be enabled so try calling your hosting provider to enable the lynx command and believe me this is better than using wget command

  3. Utsav on July 5th, 2008 4:47 am

    aaahhh!!! something i was searchin for.

    Will try this and see if it works for me. Thanks man

Leave a Reply