Learned a little bit about cron and php

Learned a little bit about getting a PHP script to run through the cron today. Also learned alot about cron itself. It was pretty interesting but very confusing at times. My site has a GUI interface for the cron so it is pretty easy to set up something. But what about if my next site doesn’t – Ex: if I am on a dedicated server. What would I do then to get my PHP script to run every day at midnight and have any messages output by it sent to my email. And as a friend told me – what if the server itself has an error – how will I be told about that?

Here is how my learning experience went:

Started out just looking for a tutorial to tell me how to run a PHP script as a cron. Found this tutorial. Really easy to understand but rather basic. Cool. But I want to make sure what I’m doing is correct so I keep looking and find this tutorial. It is a little more advanced and gives a lot more detail about what everything means. Great! Now I think I have it. So I start coding the rest of the script.

(I am a member of the PHPCommunity volunteers so I commonly hang out on it’s IRC channel.) So I’m talking to some of the other guys on the channel and make the comment that I am writing a cron script. We start talking about it and somehow we get on the subject how I want to have the cron command line code for running the script in case there is no GUI. ebag tells me I should have the output emailed to myself incase anything goes wrong. He can’t remember how to do it, but he knows it is possible.

Since I was right in the middle of using PHP’s mail() to tell me if an error occurs in the script – that sounded like a good idea :) . So off I go investigating deeper. Eventually find this site. And what do you know it shows me how to have the output emailed to me. Edit my cron command again. Now I’m feeling pretty proud of myself and very thankful to ebag for telling me it was possible.

Show the guys the newest version of the command and ebag suggests I make one more addition so I can also have the server’s standard errors sent to the email. Since I have zero knowledge about servers he sends me another link to red up on it and understand (sorry – didn’t keep that link). So now I finally get it.

And what is the command? :)

0 0 * * * /usr/local/bin/php /path/to/your/cronscript.php 2>&1 | mail -s "Subject of mail" person@domain.com

Thanks ebag for all of your help. I really appreciate it!

Comments (3)

salimJanuary 16th, 2006 at 11:49 am

i like this site

chucksJanuary 16th, 2006 at 11:52 am

i love u

Edit: Why thank you – but I have to tell you I’m married. ;)