Running PHP by Cron
MatthewHSE
Status: Contributor
Joined: 20 Jul 2004
Posts: 122
Location: Central Illinois, typically glued to a computer screen
Reply Quote
I've written a script or two that I'd like to run by Cron or the command line, but every time I try, I wind up getting errors. It keeps saying there's a mistake in my PHP syntax or some other similar error. Basically, it takes a script that works fine in the browser and totally rejects it by Cron or SSH.

So how do you write a PHP script that will work under those circumstances? Are there special procedures? Or perhaps different syntax rules? (That would seem strange.) One technique I read about was to eliminate the <?php and ?>, but that didn't solve my problems either.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
Hi Matthew, I've just been working on that type of thing.

To run a script through cron, you have to tell the cron thing what to use for the job.

To do that, you'd need to get the path to the php executable on the server, that's easy to do, logon to the server with SSH, then run this command:

:: Code ::
which php
or
whereis php

This will give you the path to the php executable, probably:

/usr/local/bin/php

You'll use this path to tell the cron job which program to use to run the job. Cron isn't apache, so it doesn't know by default what to do.

The cron job would look like this:

/usr/local/bin/php /usr/www/yourusername/includes/yourscript.php

It's easy to test this, just log on with ssh, and run that exact command, and see what happens.

Possible problems with cron jobs and php
If the script is going to produce output, or do something else like that, there's an extra step in the procedure, but since I don't know what you're setting up here, I won't get too much into it.

In my case, I use wget to run the job, because I need to run something in a browser for it to work, in that case, I setup a separate sh file, which is a standard way to call php scripts, the sh thing runs the command to open the browser, which then requests the php page, which is above the web root, although it may not have to be, then it creates the output, and closes itself.

At least theoretically, it hasn't been working so far, don't know exactly why.

Oh, I remember, it comes back to me, if the php script uses includes, the script has to be run through apache, which means it has to run as a webpage, which means I have to use something like wget or lynx to demand the page from the server, then the page processes through Apache since it's just a standard http request, running in Apache.

This works pretty well, I had better luck with wget than lynx by the way.

Here's the sample of the sh file called by the cron job:

:: Code ::
#!/bin/sh

/usr/local/bin/wget -q -t 5 --delete-after http://www.mysite.com/data/send_file.php

#/usr/local/bin/lynx -source http://www.mysite.com/data/send_file.php

The lynx one is for lynx, I don't use that, wget gives you more options.

Note the syntax here:
#!/bin/sh #! then the path to the sh executable, gotten by which or whereis
Then the path to the wget:
/usr/local/bin/wget
Then some switches:
-q quiets output, for testing purposes delete this until you have it running, that will give you error output without it. None with it.
-t 5 is how many retries before giving up
--delete-after shuts it down after the job runs.

Then the web accessible path to the php file.

If the file has no includes, you don't need to do this stuff, since you don't need apache's configurations for anything.

If you never use the file or any other modules on the website, you can skip the sh part, and just hard code in the include paths, absolute paths to the server root, not the site root.
Back to top
MatthewHSE
Status: Contributor
Joined: 20 Jul 2004
Posts: 122
Location: Central Illinois, typically glued to a computer screen
Reply Quote
Thanks for the details. I've tried some of these things, others are new to me, but at the moment the wget method seems to be working well for me. Like you, I need most of my things to run through the browser from time to time, so I guess simulating a http request with wget is the best option anyway.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
Matthew, glad you got that working, I forgot, but setting up cron / php job was posted on a bit earlier if you missed that thread.

I think the reason lynx doesn't work as posted is that the cron job needs to shut down lynx and delete the page after it's done, don't know the command line switches for lynx, but the wget ones are pretty straightforward.

This technique is pretty useful since you can use it to create things like backups of database listings, and so on. I use it to generate a csv file from last updated fields in dbs then email those to clients, zero upkeep, setup up once and never look at again, heaven.

I'm going to start using these techniques more, once I learned about wget here, the methods started working, which is nice.

Just out of curiousity, what type of script are you firing by cron? Interesting to see what people use this stuff for.
Back to top
MatthewHSE
Status: Contributor
Joined: 20 Jul 2004
Posts: 122
Location: Central Illinois, typically glued to a computer screen
Reply Quote
Thanks for the reference to the other thread, I'll look it over soon.

The script I'm running is one I wrote to automate an "Article of the Week." It randomly grabs an article from a database, then updates two text files to print a headline, blurb and link. I use one of the text files to pull onto other pages with includes, and the other is a JS file that I allow other sites to reference on their pages to "syndicate" our weekly article. (Other methods might technically be better, but this is simple enough that just about anybody can add our articles to their site.)

As an extra little goodie, the script e-mails me upon success with the name of the article chosen, the blurb, and link. If the script fails at any point, it e-mails me a failure notice. (It's never failed yet, except during testing when I deliberately messed up a variable or two to test my error checking.)

Previously, I had to run the script every Monday morning in my browser. That was easier than doing the updates myself manually, but I wanted it automatic. I got it set up to run with wget, and it appears to be working now.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
Sounds cool, glad you got it working, I especially like having these things email the results, that's just so easy to deal with, zero work, I've started getting tired of doing all those little manual upkeep things, just am finishing up a email mailing list management component for a friend's website, done so I don't have to do most of it anymore, turned into a headache but almost finished, so never have to deal with most of the parts of that procedure again.
Back to top
MatthewHSE
Status: Contributor
Joined: 20 Jul 2004
Posts: 122
Location: Central Illinois, typically glued to a computer screen
Reply Quote
Yeah, another useful tool I created was a custom 404 page that e-mails me whenever a 404 error is encountered. It sends the time of the error, the requested page, the referring page, the UA string, remote IP, and host if applicable. It helped me locate a couple pages I'd foolishly deleted and was still linking to in some places, as well as a couple places I'd accidentally mistyped image extensions.

After that I thought I'd have no further use for it, but you'd be surprised how many major search engines try to visit pages that have never existed on my site. This gives me the opportunity to create the pages it wants to find, thus increasing the number of pages I get indexed.

The script is pretty advanced in its own way; turns e-mail notices on and off with a single variable, can turn off notices when they're generated from your IP, etc. And of course, below all the PHP stuff, you can code HTML for a site map or whatever you want visitors to have when they happen to get the 404.

It's also helped me find a few folks who were trying to hack into my stats, admin areas, etc., by guessing at the URL's.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours