Question: Looking to do something that I think is fairly basic, but having never written a CRON job before, I'm not really sure how to go about it. Basically, I have a simple DB query that I've written:

```
SELECT SUM(total) as totalDownloads FROM wp_podpress_statcounts

```

As you'd expect, this displays a number. What I'd like to do, though, is create a CRON that automatically runs this query every day and sends me the results. I'm keeping track of day-to-day downloads of a podcast, and the podPress plugin I'm using leaves a lot to be desired in the metrics department. Ideally, I'd like to build my own stats system; however, my PHP isn't quite up to snuff.

Thanks in advance!

Answer: 
No need for PHP if you use a few built-in UNIX tools:

To execute from commandline, using the [MySQL commandline](http://dev.mysql.com/doc/refman/5.5/en/mysql.html):

```
mysql -e 'SELECT SUM(total) as totalDownloads FROM wp_podpress_statcounts';

```

Either put a `-u -p` for the username and password, or put a [`~/.my.cnf`](http://dev.mysql.com/doc/refman/5.1/en/option-files.html) in your homedir.

Mail it to yourself using [UNIX mail(1)](http://compute.cnr.berkeley.edu/cgi-bin/man-cgi?mail%201):

```
mysql -e 'SELECT SUM(total) as totalDownloads FROM wp_podpress_statcounts' | mail your.addy@host.com

```

Now to [crontab](http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab%205) for each day.

Do a `crontab -e` and enter these two lines:

```
# at midnight, every day
0 0 * * *        mysql -e 'SELECT SUM(total) as totalDownloads FROM wp_podpress_statcounts' | mail your.addy@host.com

```
Just an FYI - there is one other option you could also look at - mysql events. Similar to what you would have in MS SQL Server for example. There are some limitations on what you can do so you would definitely want to check those out first.

Check out: <http://dev.mysql.com/doc/refman/5.1/en/events.html> for more info.

Personally, I often write a shell script and use cron as noted above.