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

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.

13 thoughts on “How to run a php file(script) automatically in a certain interval – Using Cron job

  1. Shakeel Shretha

    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. 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. aaahhh!!! something i was searchin for.

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

  4. Kassem Ezz

    Exactly what I was looking for…..
    10000 thx man

  5. Muthu

    I’m Using XAMPP in WindowsXP. I want to run a PHP file automatically for every 10 Mins to Send SMS.
    How can I write a program in PHP to do it in Windows OS?
    I need ur Help.

  6. hope this work… ill give it a try… :) tyvm

  7. sha

    I want to run a PHP file automatically for every 1hour to Send SMS.
    How can I write a program in PHP to do it in Linux Server?
    I want code for Send SMS
    I need ur Help.

  8. Parmendra

    hi sir,

    I have to run a php script as cron job without cpanel help.

    is it possible to run this through .htaccess file.
    if yes please reply me as cpt.parmendra@gmail.com

    Thanks
    Parmendra Kumar

  9. Jagdish

    Hi
    i also want to send my site users sms every day to give new product information.
    please ! please ! ! please !!!
    help me to give code of how to send sms by website in php language by setting cron.

  10. Hi
    is it possible to run a php file automatically
    through a php script only without server involvement.
    And how to send sms to mobiles with php…
    thank you

  11. i have tried the command that u have mentioned but it display
    /bin/sh: /usr/bin/lynx: Permission denied

    i don’t have any idea about that can u share with me.

    regards ravi karmacharya

  12. I was actually looking to see some examples of how to create a cron entry froma Php function.
    But its ok, i figured it out, using shell

  13. thankx for all

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>