I used Cron to generate daily report (writen in PHP) of my website and send the report to my email inbox everyday automatically. Basically, Cron a utility in Unix/Linux that enables users to execute commands or scripts (groups of commands) automatically at a specified time/date.
Since I don’t have access to the shell of my web hosting’s server, I have to use the crontab service (web version) provided by my web hosting to schedule a cron job.
Note: You need to check with your service provider whether they provide the crontab service.
This is how I generate the daily report for my website:
- Write a PHP script that connect to mysql database, update some stats, retrieve some records and display it nicely either in html format or plain text format (personally I would prefer plain text format).
- Schedule a Cron job at a specific time (e.g. 1.00am) for the PHP script just now. The command might look like this:
lynx -dump http://www.mysitename.com/genReport.php
. - Type in my email address and save the Cron job.
Everyday at about 1.00am, a report will be delivered into my inbox automatically. In order to execute the command above, you need to have the right to access to lynx
command (Again, you will need to check with your service provide about this).
Note: Lynx is a text browser for the World Wide Web.
Normally, I will make the cron command looks like this: lynx -dump http://www.site.com/report.php?magicnumber=43214321
. So that in my PHP script, if it does not read the magicnumber
with value 43214321
, it immediately exit the script. This to avoid the execution of the script when someone type the url in browser or when web crawler visit the page. For example:
< ?php
if (!(isset($_GET['magicnumber']) && $_GET['magicnumber'] == '43214321')) die;
?>
Here are some of my ideas on what you can do with cron + lynx + php + mysql:
- Purge unused transactions daily/weekly/monthly to keep database size small.
- Make datebase updates.
- Generate daily/weekly/monthly reports from the data of your own database.
- Capture daily/weekly/monthly reports from other websites (e.g. Google Adsense – this is in my project list) and send the reports to your inbox daily/weekly/monthly.
- Automatic generate “Weekly Highlights” post for blog/forum/news site
Edrei says
Umm…well since I noticed that you’re using WordPress, I’m surprised that you didn’t use the WP-Cron plugin. I use it to scheduel weekly backups of my WordPress DB. It works fine for all plugins running on my site.
szehau says
Ya.. It is a great plug-in for wordpress. I just visited the site and downloaded the source code. I noticed that the scheduler only work on a “roughly” time. The scheduler only works when someone is browsing your site. Thus, it is not suitable for jobs that need to be executed in a very specific time (e.g. hourly). The plug-in works best on a busy site.
The report I mentioned above is for my other website (not this one) which make updates to database and generate report on a daily basis. Anyway, thanks for your comment and suggestion.