Tim
05-19-2008, 12:47 AM
[repost by request of darkneoboi]
What in the world is a cron job? A cron job – meaning chronological job – is a task for the server. You set the times and dates to do certain tasks, just like Window's Task Manager. You can have it do a lot: running a shell script, perl script, php code, and more. We're going to look at php. By using cron and php you can have yourself a site that automatically updates caches, daily weather predictions, hourly schedules, etc. with a bit of simple code.
We're first going to make a php script.
#!/usr/local/bin/php -q
The above tells the server what to do with the script. Normally when you run a php script from a browser you don't need this line, but because we're using cron we need to give some extra instruction to the server. It pretty much says “Here is the location to the php library,” so that the server knows to execute it in php, and not in its own way.
<?php
$handle = fopen(“lastupdated.txt”, “w”);
fwrite($handle, date(“r”));
fclose($handle);
?>
What this does should probably not be used for an actual purpose; but for an example's sake: It opens up the file “lastupdated.txt” and writes the current date RFC 2822 (http://www.faqs.org/rfcs/rfc2822) format. You can put this in any directory of your site that you'd like. However, it's probably best to put it in the parent of public_html, so that it can't be run by someone who wants to do something to your site and it can only be run by the server itself.
Since we're using cPanel, the cron tab, or cron file, is very easy to edit. First, log in to cPanel and find the icon that says “Cron jobs”:
http://img132.echo.cx/img132/2884/cron5sp.png
Once we're there, we have two options, Standard and Advanced. Advanced is “unix style” because crons originated on unix systems. On a unix pc you would need to edit the cron tab by hand. The advanced option gives you the same feel of a unix cron tab. If you are familiar with cron you can use that, otherwise we'll stick to Standard in this tutorial.
If you click standard the form should look like this:
http://img197.echo.cx/img197/2921/cronint2ri.png
In the first field, email, you should put your own email address. The default will most likely be your cPanel username, however, this will send email to your cpanel mail account, and unless you use that often it would be best to set it to your own private email to get updates. If your cron script does not output anything using print, echo, or a similar function you should not receive an email, however, if it does than you will get one.
Under Entry 1 your command will look like:
php -q /home/user/file.php
This tells the server, again to use php. -q means that the server should suppress headers sent to by php – this makes it so if you don't echo anything you won't get an email. Finally, the last bit is the path to the script. /home/ is basicly the root cPanel directory. Under that is /user/, your cPanel user. Finally, file.php is whatever you named your cron php file. In the field Minute(s) you should set the minutes of the hour to run your cron. To select multiple you should hold down control and select them that way. I want my cron to run at 6:30 PM and 7:30 PM every day, so I click the 30 in the minutes field. Under hour I need to select 6 and 7. Since it's PM it's really 18 and 19 on a 24-hour clock. Therefor, I select 6, hold control, and select 7. Days are the days of the month you want to perform your task. In my case I want it to run on the first day of the month only, so I pick the #1. We want all months, but you can obviously select only certain months. Again, I'm going to choose every weekday, because I don't know what day the first day of the month is going to be. And that's that. Scroll to the bottom and press “Save Crontab.”
Wait until the times/and dates you selected, and see if it runs :). Crons are very reliable, except when the server is down, so lastupdated.txt will most likely be updated.
Crons are a very simple, and very useful tool when used correctly, and can make your site even easier to manage!
What in the world is a cron job? A cron job – meaning chronological job – is a task for the server. You set the times and dates to do certain tasks, just like Window's Task Manager. You can have it do a lot: running a shell script, perl script, php code, and more. We're going to look at php. By using cron and php you can have yourself a site that automatically updates caches, daily weather predictions, hourly schedules, etc. with a bit of simple code.
We're first going to make a php script.
#!/usr/local/bin/php -q
The above tells the server what to do with the script. Normally when you run a php script from a browser you don't need this line, but because we're using cron we need to give some extra instruction to the server. It pretty much says “Here is the location to the php library,” so that the server knows to execute it in php, and not in its own way.
<?php
$handle = fopen(“lastupdated.txt”, “w”);
fwrite($handle, date(“r”));
fclose($handle);
?>
What this does should probably not be used for an actual purpose; but for an example's sake: It opens up the file “lastupdated.txt” and writes the current date RFC 2822 (http://www.faqs.org/rfcs/rfc2822) format. You can put this in any directory of your site that you'd like. However, it's probably best to put it in the parent of public_html, so that it can't be run by someone who wants to do something to your site and it can only be run by the server itself.
Since we're using cPanel, the cron tab, or cron file, is very easy to edit. First, log in to cPanel and find the icon that says “Cron jobs”:
http://img132.echo.cx/img132/2884/cron5sp.png
Once we're there, we have two options, Standard and Advanced. Advanced is “unix style” because crons originated on unix systems. On a unix pc you would need to edit the cron tab by hand. The advanced option gives you the same feel of a unix cron tab. If you are familiar with cron you can use that, otherwise we'll stick to Standard in this tutorial.
If you click standard the form should look like this:
http://img197.echo.cx/img197/2921/cronint2ri.png
In the first field, email, you should put your own email address. The default will most likely be your cPanel username, however, this will send email to your cpanel mail account, and unless you use that often it would be best to set it to your own private email to get updates. If your cron script does not output anything using print, echo, or a similar function you should not receive an email, however, if it does than you will get one.
Under Entry 1 your command will look like:
php -q /home/user/file.php
This tells the server, again to use php. -q means that the server should suppress headers sent to by php – this makes it so if you don't echo anything you won't get an email. Finally, the last bit is the path to the script. /home/ is basicly the root cPanel directory. Under that is /user/, your cPanel user. Finally, file.php is whatever you named your cron php file. In the field Minute(s) you should set the minutes of the hour to run your cron. To select multiple you should hold down control and select them that way. I want my cron to run at 6:30 PM and 7:30 PM every day, so I click the 30 in the minutes field. Under hour I need to select 6 and 7. Since it's PM it's really 18 and 19 on a 24-hour clock. Therefor, I select 6, hold control, and select 7. Days are the days of the month you want to perform your task. In my case I want it to run on the first day of the month only, so I pick the #1. We want all months, but you can obviously select only certain months. Again, I'm going to choose every weekday, because I don't know what day the first day of the month is going to be. And that's that. Scroll to the bottom and press “Save Crontab.”
Wait until the times/and dates you selected, and see if it runs :). Crons are very reliable, except when the server is down, so lastupdated.txt will most likely be updated.
Crons are a very simple, and very useful tool when used correctly, and can make your site even easier to manage!