Apr
19

Schedule Automatic Cron Jobs of *.PHP Scripts

As a hacker, I have to program my remote web server to automatically perform routine tasks for me. It's a whole lot easier and cheaper to rely on my web hosting company to manage an always-on server. For starters, I don't have to pay for the 24/7 electricity the on-call technician!

To make the server perform an automated task in a Linux or Unix environment, you need to schedule a cron job. I won't go through all the details of setting it up in shell terminal since most people do not have remote shell access. You can read about the details more at the cron and crontab Wikipedia articles. Most of us deal with an interface on the cPanel that looks kind of like one of these two screens.

cronphp_standard.jpgcronphp_advanced.jpg

 

Do I Run the PHP Text File or a Program?

Scheduling can be easy enough if you follow the on-screen instructions. But when the form prompts you for the command what do you input? The first impulse would just be to feed the URL or file path of the PHP file to cron. That will not work. To cron and to Linux, a PHP file is just a simple text file. Cron needs a program to parse, interpret, and run the commands in the text file. In a cron job, you need to specify a script or an executable file to run. That's easy enough if you know how to write Unix scripts. But, I sure do not have any idea! I only know how to program in PHP, HTML, JavaScript, and a little C/C++. For web programmers with the knowledge of modern server side scripting like PHP, there is hope.

There are three ways you can do this. The first way is to actually call the PHP interpreter binary to run the PHP script. The second way is to call a text-based browser for terminal environments. The last way is to call an HTTP retriever. To see which one works for you, create a PHP file that emails your Inbox when it is successfully executed.

PHP:
  1. <?php
  2. //Filename: cron.php
  3. mail ("my@email.com", "Cron Successful!", "Hello World!");
  4. ?>

PHP Interpreter

This will only work if your server parses PHP files as a CGI directive. That is, PHP is not installed as an Apache module. Most newer web server setups utilize PHP via the Apache module since it is a more secure setup. Therefore, this may not apply to you. If you want to test it out, cron the above PHP script with this:

/path/to/php /path/to/your/cron.php

or

/path/to/php http://www.mydomain.com/path/to/your/cron.php

Text Browser

If referring to the PHP interpreter does not work, you will need to call a text web browser to open the file, thus forcing the HTTP daemon to run the script. There are many text browsers out there. The most popular one is Lynx. It should be included on most *nix environments.

/path/to/lynx http://www.mydomain.com/path/to/your/cron.php

HTTP Retrievers

Apparently, my web host removed the lynx command so I was forced to look elsewhere. My last resort was HTTP retrievers like "get" and "wget." Basically, these binaries "get" the file from the HTTP daemon like text browsers and usually save it somewhere.

/path/to/get http://www.mydomain.com/path/to/your/cron.php

or

/path/to/wget http://www.mydomain.com/path/to/your/cron.php

Good luck! If you have any question or suggestions, please leave a comment!

If you enjoyed this post, make sure you subscribe to hacker not cracker via RSS feed or email update!



Additional Reading

Comment View Comments from Other Readers

Popular Posts

Featured Posts

Related Posts

Recent Posts

What's Your Reaction?


Subscribe to this Blog:

Reader Reactions Elsewhere


 

18 Responses to “Schedule Automatic Cron Jobs of *.PHP Scripts”

  1. Rob Says:

    Nice round up of options.

    If I may add a couple of points:

    On my host (and presumably others) using wget stores the file in my home dir. For scripts that run once a day this can quickly lead to loads of files that need to be cleared out. Routing the output of cron to /dev/null (e.g. wget http://www.domain.com/myscript.php > /dev/null & 2 ) prevents this.

    Also depending on your setup you can call php as a script (just like any other shell script) by adding the shebang #!/path/to/php to the top of the file and chmoding it to be executable. You can then execute it directly.

    Rob

  2. blogger Says:

    Thanks for your suggestions. The thought of HTTP retrievers dumping the output in a file did not even cross my mind. Thanks for mentioning it. I guess I better check my home directory if it is bloated with the byproduct of my cron jobs! :)

  3. Farreina Says:

    How do I execute the CRON job of PHP script on Windows? I'm newbie.

  4. Manish Says:

    Farreina, I am also want to know to do it for windows. Any body there who can help us?

    Regards,
    Manish Patel

  5. Nobin Says:

    I tried with cpanel Cron Jobs. but it send a email with access denied.

    Why this happend?

  6. aika Says:

    Under windows you should add a new tas in task sheduler:
    \

  7. aika Says:

    ok, somebody cut my text
    task: path.to.browser.exe http.path.to.cron.php

  8. madkutznews.blogspot.com Says:

    I for all time emailed this blog post page to all my friends, as if like to read it next
    my contacts will too.

  9. Testosterone Booster Supplement Says:

    Wow, this post is good, my sister is analyzing these things,
    so I am going to tell her.

  10. aging skin treatment Says:

    I do agree with all the concepts you've introduced
    to your post. They are really convincing and can certainly work.
    Still, the posts are very short for newbies. May just you please extend them
    a bit from next time? Thank you for the post.

  11. Weight Loss Supplement Says:

    I think everything posted made a lot of sense.
    But, think on this, suppose you composed a catchier post title?
    I am not suggesting your information is not good,
    however suppose you added a headline to possibly
    get a person's attention? I mean Schedule Automatic Cron
    Jobs of *.PHP Scripts - hacker not cracker is kinda vanilla.
    You ought to glance at Yahoo's front page and note how they create article titles to grab viewers interested.
    You might add a video or a related picture or two to get people interested about everything've got to say.
    Just my opinion, it would make your website a little livelier.

  12. andujar.es Says:

    Steven Scott is a Maui-based freelance travel writer and photographer and can be reached at thehawaiitourist@gmail.
    Once they are VPi - X certified, we can then get them to service our national accounts.
    The perfect escape can be had at this quiet, condo-type resort with sprawling grounds right in front of Kaanapali Beach.

  13. About | خريد vpn| خريد کريو| خريد ساکس| خريد وي پي ان Says:

    About | خريد vpn| خريد کريو| خريد ساکس| خريد وي پي ان...

    I saw this really great post today....

  14. Buy HGH Pro Says:

    I am curious to find out what blog platform you have been working with?

    I'm experiencing some minor security issues with my latest site and I'd like to find something more secure.
    Do you have any suggestions?

  15. testerone booster Says:

    If you need more information of the above subject, please
    visit my home page at:. In his clinic, he used suspension from pulleys, racks, moving manipulation and different uncommon merchandise for addressing rheumatism and inflammatory disease, basic cognitive process that rheumatism originates from your vertebrae.
    Catch more carp and beat leading brand readymade boilies
    with your own winning versions.

  16. http://dapalma.co.uk/profile/curt93e Says:

    It is a dead air space which provides the greatest insulating capacity.
    Soft, silky skin is always nicer than being hairy and
    dry like a caveman. Calling up your supervisor outside of work hours becomes inevitable once in a while.
    Know the difference between what makes a good salesperson for your product and what makes a good
    salesperson for your company"s long-term future. You start asking yourself constantly, how can this get done without me.

    Also visit my web site - colocation hosting web (http://dapalma.co.uk/profile/curt93e)

  17. nancy pelosi Says:

    Many people are already making the leap with
    renewable energy sources and downsizing their 'needs.
    He had no knowledge that the aged guy at the feed retailer
    had a good humorousness. Protecting these eggs from
    predators and nosy neighbors is the one other consideration.

    Here is my web-site :: nancy pelosi

  18. garcinia cambogia total lean side effects Says:

    Helpo there! Do yoou use Twitter? I'd like to follow you if that
    woould be ok. I'm definitely enjoying your blog and look forward to
    new posts.

    Here is my page :: garcinia cambogia total lean side effects

Leave a Reply

 
Latest Post on Loading...: Please Wait...
admin admin
© 2006 and web design of Allan Ray Barizo from [art] [⁄app].
This site is best viewed with FF and at least 1024x768 resolution.