创建 CRON 作业来获取/发送数据库值
想做一些我认为相当基本的事情,但以前从未编写过 CRON 作业,我不太确定如何去做。基本上,我编写了一个简单的数据库查询:
SELECT SUM(total) as totalDownloads FROM wp_podpress_statcounts
如您所料,这显示了一个数字。不过,我想做的是创建一个 CRON,每天自动运行此查询并向我发送结果。我正在跟踪播客的日常下载,而我使用的 podPress 插件在指标部门还有很多不足之处。理想情况下,我想建立自己的统计系统;然而,我的 PHP 还不够好。
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用一些内置 UNIX 工具,则不需要 PHP:
要从命令行执行,请使用 MySQL 命令行:
输入
-u -p
作为用户名和密码,或者输入~/.my.cnf
在你的主目录中。使用 UNIX 邮件(1) 将其邮寄给您自己:
现在到每天的crontab。
执行
crontab -e
并输入以下两行:No need for PHP if you use a few built-in UNIX tools:
To execute from commandline, using the MySQL commandline:
Either put a
-u -p
for the username and password, or put a~/.my.cnf
in your homedir.Mail it to yourself using UNIX mail(1):
Now to crontab for each day.
Do a
crontab -e
and enter these two lines:仅供参考 - 您还可以查看另一个选项 - mysql 事件。例如,类似于 MS SQL Server 中的内容。您可以执行的操作有一些限制,因此您肯定需要先检查一下这些限制。
查看: http://dev.mysql.com/doc/refman/ 5.1/en/events.html 了解更多信息。
就我个人而言,我经常编写 shell 脚本并使用上面提到的 cron。
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.