• 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

July 16, 2005 01:58 by Sze Hau 2 Comments

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

More from my site

  • Received My Free Ubuntu 9.04 Server Edition CDReceived My Free Ubuntu 9.04 Server Edition CD
  • My New Camera – Panasonic Lumix TZ5My New Camera – Panasonic Lumix TZ5
  • Sushi King’s Mini Yee SangSushi King’s Mini Yee Sang
  • GIMP – The GNU Image Manipulation ProgramGIMP – The GNU Image Manipulation Program
  • Happy Earth DayHappy Earth Day
  • Beware of Phishing Site Links from Windows Live MessengerBeware of Phishing Site Links from Windows Live Messenger

Filed Under: programming

About Sze Hau

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

Comments

  1. Edrei says

    July 16, 2005 15:54 at 3:54 pm

    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.

    Reply
  2. szehau says

    July 16, 2005 20:24 at 8:24 pm

    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.

    Reply

Leave a Reply to szehau Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Email News Letter

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

  • Facebook
  • Google+
  • Instagram
  • Twitter

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 Leave a Comment

LED Downlight

Troubleshooting A Flickering LED Downlight

February 13, 2016 00:29 By Sze Hau Leave a Comment

Repair A Broken iPhone Lightning Cable

January 30, 2016 00:54 By Sze Hau Leave a Comment

Sponsored Links

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

szehau’s weblog

szehau is a software programmer that has strong interest in the world of programming and Internet. He is graduated in University of Malaya, Malaysia with a Bachelor’s degree in Software Engineering. He draws on his passsion for web programming to share with others how to build a successful websites.

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

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

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in