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