• Home
  • About

szehau's weblog

Life, Internet, Software, Gadgets, Programming and Investments

  • life log
  • internet
  • programming
  • software
  • working holiday
  • gadget
  • project

Running PHP Scripts with Cron

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:

  1. 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).
  2. 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.
  3. 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:

  1. Purge unused transactions daily/weekly/monthly to keep database size small.
  2. Make datebase updates.
  3. Generate daily/weekly/monthly reports from the data of your own database.
  4. 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.
  5. Automatic generate “Weekly Highlights” post for blog/forum/news site

About Sze Hau

Geek. Love programming. Coffee addicted. Married with a child. Working towards financial freedom.

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

More Posts

5 Essential Things You Need To Have At Your Home During The COVID-19 Pandemic

June 1, 2021 22:15 By Sze Hau

LED Downlight

Troubleshooting A Flickering LED Downlight

February 13, 2016 00:29 By Sze Hau

Repair A Broken iPhone Lightning Cable

January 30, 2016 00:54 By Sze Hau

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • Snippet IT Information technology news, ideas and experiences and opinions

Recent

  • 5 Essential Things You Need To Have At Your Home During The COVID-19 Pandemic
  • Troubleshooting A Flickering LED Downlight
  • Repair A Broken iPhone Lightning Cable
  • New Gadget – MacBook Pro with Retina Display (2015)
  • One Watt LED Night Light

Search

Tags

2014 adsense advertisement Arduino baby blog blogtal Canon EOS 400D Chinese New Year Christchurch CNY DiGi DIY electronic FBM Flickr Flickr Uploadr Google Google Maps Google Search Results Google Webmaster Central investment joke KLCI KLSE Lake Tekapo LED MacBook Air Maybank2u Motueka movie mutual funds New Zealand PC Fair 2009 Queenstown restaurant for dinner sharing stock trackback Twitter unit trust visa wordpress wordpress plugin working holiday