PDA

View Full Version : Can you use CRON to run PHP files?


HalfaBee
3-28-02, 05:18 PM
I want to run some PHP scripts on a regular basis. Has anyone used cron to do this?

I need a bit of help.

Thanks Halfabee

wwuud
3-28-02, 10:02 PM
I *think* that you can, but it may not be worth the trouble to learn how to set it up. Some possible alternatives that i use:

Internet Explorer can "make available offline" any page in the "favorites" list. You can schedule it to load at regular intervals.


Create the page with a "reload" meta tag with a *really* long timeout (a few hours) and load this page up in a browser window. You can do creative things with framesets with this method.

Use the Windows Task Scheduler to load a URL shortcut. I've written MS-DOS .bat batch files that do a MySQLdump, date-stamp-rename with an ancient command-line utility called fdate, and zip into a daily-back-up archive with the DOS-version PKzip. The old DOS batch files were never really superceeded within Windows (use Windows scripting at your own peril!!)
These all, of course, are not perfect fixes and are dependant on a more-or-less permenant connection (i.e. dsl, cable or better).

HalfaBee
3-29-02, 06:04 PM
I found that you can run cron on PHP files. The format is very simple and I thought it might help other people.

In etc/crontab just add a line for the job you want
format

MM HH DD MM DOW job

MM=minutes
HH=Hours
DD=day
MM=Month
DOW = Day Of Week

I just put job.php as the job and it worked.
My biggest problem was working out what time the cron daemon was running on.
Believe it or not the file creation date was not the same time as cron.
A file created at 22:53 needed 14:53 as the job time.
Pretty wierd but it worked.

Hope this helps someone.
HalfaBee

firebolt
11-18-02, 11:22 PM
Hi Halfabee,

I tried:
0 ***** /www/f/firebolt/htdocs/crontest.php >>/www/f/firebolt/logs/cron.log 2>>/www/f/firebolt/logs/cron.err

(all as one line)

I added the above to my site yesterday and it didn't work. How should I change it to make it work? It looks like from your post the putting extra spaces in would get it to work, but I want to make sure since waiting every day to "debug" cron isn't something I want to do all week. =) Thanks!

Confusion
11-27-02, 09:22 AM
Lots of programmers like PHP for its ability to code and develop web applications fast.
Code-debugging is a lot easier than with PERL or C. However, there is one thing a lot of developers are puzzled about,
"How to run PHP Scripts with crontab?"

Cron is normally available on all Unix and Linux distributions and is available on Powweb although it is not supported.
It is a daemon which allows you to schedule a program or script for a specific time of execution.
If you want to learn more about cron, click here <http://uwsg.iu.edu/usail/automation/cron.html>
or type "man crontab" at your command prompt.

I have found myself in the need to run PHP scripts at specific times. For example, to update the content of a website,
to remove expired articles, to send out e-mails on a given date and a lot more.
While some may think that this is where PHP is doomed, I will show you how it's done.

A Manual crontab?
The first solution that came to my mind was to run the script directly from my browser (e.g. http://www.mydomain.com/script.php).
Since I need to run my script on a regular basis, I squashed that idea. My goodness, all the extra hassle is ridiculous.

An include?
Another possible solution is to include the script in one of the pages of the site, for example the very first: "index.php".
(<? include "cron.php"; ?>)

The drawbacks to this solution are, that it works but when someone accesses the "index.php".
This could cause a lot of extra overhead produced by the script.
If you get a lot of traffic, the script is executed 1000 times a day and adds a lot of usage on the database and the server.
On the other hand, if you do not get a lot of traffic, or people tend to access your site over another file, this will not work out as well.
If you need to run the script on a regular intervals, this is not a solution.

Crontab!
Let's suppose you either know what cron is or have read about it using the link above. We want to run our script once every five minutes.
So where do we go from here? Here is how you can accomplish this task.

Our PHP setup

Ok, so we know that our PHP is installed as CGI on Powweb.
You need to add a line to your PHP script.
It has to be the first line of your script and must contain the server's PHP executable location:
This is ours,
#!/usr/local/lib/php -q

That looks a lot like PERL now, doesn't it? After that let's add the necessary command to our crontab.
Edit /etc/crontab and add the following line:

5 * * * * php /www/id/yourusername/path/script.php
Don't forget the extra carriage return

Be sure your "script.php" has the necessary permissions to be executable ("chmod 755 script.php").

Now you are all set!
:D

Rico
11-27-02, 09:47 AM
Hello !

Do you speek a little bit french (or you can understand a french site ) ?

Because, we have A GREAT SITE to do cron works very easyly :

http://www.webcron.org

After signup, you can specify php files on your server to visit every day, week, month...

This site is gratis - no money. And very easy and fast to realize..

HalfaBee
11-27-02, 04:03 PM
Hi Firebolt,

You need spaces between the *'s and only 4 *'s the first number uses one of them.

Make sure you have
#!/usr/local/bin/php
as the first line of your script and CHMOD to 755

Also make sure you have a blank line or two at the end of the command.

Hope it works
HalfaBee

firebolt
11-27-02, 04:24 PM
Hey guys,

Thanks for your help. I've had my CRONs running nicely for a week or so now. One thing that caused me issues was saving the files in DOS format not UNIX because the line feeds messed things up, but now everything is working great. Thanks for all your help!