创建 CRON 作业来获取/发送数据库值

发布于 2024-11-30 02:58:42 字数 307 浏览 0 评论 0原文

想做一些我认为相当基本的事情,但以前从未编写过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

数理化全能战士 2024-12-07 02:58:42

如果您使用一些内置 UNIX 工具,则不需要 PHP:

要从命令行执行,请使用 MySQL 命令行

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

输入 -u -p 作为用户名和密码,或者输入 ~/.my.cnf在你的主目录中。

使用 UNIX 邮件(1) 将其邮寄给您自己:

mysql -e 'SELECT SUM(total) as totalDownloads FROM wp_podpress_statcounts' | mail [email protected]

现在到每天的crontab

执行 crontab -e 并输入以下两行:

# at midnight, every day
0 0 * * *        mysql -e 'SELECT SUM(total) as totalDownloads FROM wp_podpress_statcounts' | mail [email protected]

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

To execute from commandline, using the MySQL commandline:

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 in your homedir.

Mail it to yourself using UNIX mail(1):

mysql -e 'SELECT SUM(total) as totalDownloads FROM wp_podpress_statcounts' | mail [email protected]

Now to crontab 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 [email protected]
冰魂雪魄 2024-12-07 02:58:42

仅供参考 - 您还可以查看另一个选项 - 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文