Quick Search for:  in language:    
PHP,article,explains,script,cron,depending,yo
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
PHP Stats

 Code: 112,626. lines
 Jobs: 41. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for PHP.
DB Selector
By Martyn Merrett on 1/17


Click here to see a screenshot of this code!Pilot Group Affiliate Program 1.5.2
By PIlot Group on 1/16

(Screen Shot)

MapQuest Link Generator
By Steve Schoenfeld on 1/15


Click here to see a screenshot of this code!A Simple FTP Solution
By Marcelo Valle Franco on 1/15

(Screen Shot)

Web Blog / Content Updater
By Matthew Sparrow on 1/14


Current Users Online v1
By Robert Peterson on 1/13


Click here to see a screenshot of this code!Blobsy : MSN Messenger Bot
By JawishHameed on 1/12

(Screen Shot)

Click here to see a screenshot of this code!Server-Side Browse for Image (with GUI)
By Tohir Solomons on 1/11

(Screen Shot)

Click here to see a screenshot of this code!Automatically Add a Watermark to an Image
By Patrick Krecker on 1/11

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Running PHP scripts as cron jobs

Print
Email
 

Submitted on: 7/30/2001 3:39:59 PM
By: Gianluca Baldo  
Level: Beginner
User Rating: By 2 Users
Compatibility:PHP 3.0, PHP 4.0

Users have accessed this article 14402 times.
 

(About the author)
 
     This article explains how to run a PHP script as a cron job, depending on your PHP installation.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Running PHP scripts as cronjobs

I personally found myself in the need to run some PHP scripts I wrote, every X minutes, or evey day at a certain time and so on.
One example: phpauction package includes a script (cron.php) which is responsable for detecting expired auctions, close them (thus updating database tables accordingly), sending e-mails (to the seller and eventually to the winner of the auction), writing into a log file and more.
I use to run this script every minute in the sites where I use phpauction.

The first solution if you have such a problem would be to run the script directly from the browser (http://www.somedomain.com/cron.php) but if you really need and want to run your script regularly, this solution becomes ridiculos.

A second possible approach is to include the script in one of the pages of the site (i.e. index.php). Something like:



at the very top of index.php. This works but only if someone accesses our index.php page. Again, if you want regularity, this is not a real solution.

Cron is usually available in all Unix/Linux installations. It is a daemon which allow you to schedule programs and scripts execution. If you want to read something more about cron, go here.

Let's suppose you know what cron is and how it works. We now have to run (following the cron.php sample) our script, let's say, every minute.
The first dilema depends on your PHP installation:

  • Compiled CGI
  • Apache module

Compiled CGI
In the first case you'll have to add a line to your script. It must the first line of your script and must contain PHP executable location:

#!/usr/local/bin/php -q

After that let's add the necessary command to our crontab. Edit /etc/crontab (or wherever your crontab file is) and add the following line:

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

execute the following from the command line:

Shell> crontab crontab

Be sure cron.php has the necessary permissions to be executable.


Apache module

If you have PHP installed as an Apache module the approach is quite different.

First of all you need access to lynx (read here for more information).
You PHP script does not need additional lines in this case. You simply have to edit your /etc/crontab file and add the following line:

* * * * * lynx -dump http://www.somedomain.com/cron.php

Note you generally have to specify the compete URL: depending on your lynx configuration the URL should be relative but I suggest to always use the absolute reference like in the example above, since it always works.

Again execute the following from the command line:

Shell> crontab crontab

That's it.


Other 2 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
8/6/2001 2:57:40 PM:Timo Schlegel
I do the same (Apache module) on my server, so I get every day a mail with the current statistics and free space,...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/20/2001 7:57:10 AM:Mary Lacey
Thank you for your help with cron. I have been trying to get it to work and now I think I have it!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/20/2001 7:58:21 AM:Mary Lacey
Thank you for your help with cron. I have been having trouble getting it right but now I think I have it!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/11/2001 11:00:12 PM:Dave Minor
For Win32 users, the BadBlue PHP server has an easier version of cron built in. Free download available. Read about scheduling here: http://badblue.com/helpsch.htm and configuring for PHP here: http://badblue.com/helpphps.htm
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/17/2002 3:59:36 PM:Tom McCrary
I couldn't get the Lynx Dump to work, so I created a perl script to do the same thing... #!/usr/bin/perl use LWP; $data=
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/28/2003 4:21:47 AM:
I can't get the Lynx -dump to work, either. I get
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/28/2003 4:23:10 AM:
I can't get the Lynx -dump to work, either. I get a message saying I can't clear the screen and position my cursor. My PHP is an Apache module. Any suggestions?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/27/2003 3:18:58 AM:Gianluca Baldo
With some versions of lynx you get the
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | PHP Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.