cron 作业或 PHP 调度程序
我使用 MYSQL 作为我的数据库,PHP 作为我的编程语言。我想运行一个 cron 作业,该作业将运行直到当前系统日期与我的数据库表中名为“PROJECT”的“截止日期(日期)”列匹配。一旦日期相同的是,必须运行更新查询,这会将状态(项目表的字段)从“打开”更改为“关闭”。
我不太确定 cron 作业是否是最好的方法,或者我可以使用触发器,或者可能是其他东西。此外,我使用 Apache 作为我的 Web 服务器,我的操作系统是 windows vista。
最好的方法是什么? PHP 调度程序或 cron 作业或任何其他方法? 有人可以启发我吗?
I am using MYSQL as my database and PHP as my programming language.I wanted to run a cron job which would run until the current system date matches the "deadline(date)" column in my database table called "PROJECT".Once the dates are same an update query has to run which would change the status(field of project table) from "open" to "close".
I am not really sure if cron jobs are the best way or I could use triggers or may be something else.Also I am using Apache as my web server and my OS is windows vista.
Also which is the best way to do it? PHP scheduler or cron jobs or any other method? can anybody enlighten me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
cron 本身在 Vista 中并不存在,但存在的是标准的 Windows 调度管理器,您可以使用“php -q -f myfile.php”之类的命令行运行它,它将在给定时间执行 php 文件。
您还可以使用 cron 程序的端口,有很多。
如果对第二个并不重要,任何 Windows 调度应用程序都可以,只是为了简单起见,请确保在 PATH 变量中包含 PHP bin 路径。
cron does not exist, per se, in vista, but what does exist is the standard windows scheduling manager which you can run with a command line like "php -q -f myfile.php" which will execute the php file at the given time.
you can also use a port of the cron program, there are many out there.
if it is not critical to the second, any windows scheduling application will do, just be sure to have you PHP bin path in your PATH variable for simplicity.
对于 Windows CRON 作业,我强烈推荐 PyCron 。
For Windows CRON jobs I cannot recommend PyCron enough.
虽然 CRON 和 Windows 计划任务是安排作业/任务定期运行的可靠方法,但在某些用例中,在 CRON/Windows 中拥有不同的计划任务可能会变得乏味。 也就是说,当您想让用户安排运行的事情时,或者您喜欢简单性/可维护性/可移植性/等等或以上所有的情况下。
如果我不想使用 CRON/Windows 来执行计划任务,我会在应用程序中构建一个任务计划系统。 这仍然需要安排 1 个 CRON 作业或 Windows 任务。 这个想法是将作业详细信息存储在数据库中(作业名称、作业属性、上次运行时间、运行间隔以及对实现重要的任何其他内容)。 然后,您在 CRON 或 Windows 中安排一个“主”作业,它会为您处理运行所有其他作业。 您需要这个主作业至少按照您最短的间隔运行; 如果您希望能够安排每分钟运行的作业,则主作业需要每分钟运行一次。
然后,您可以轻松地从 PHP 在后台启动每个计划的作业(如果您愿意)。 在内存受限的系统中,您可以监视内存使用情况或跟踪PID(各种方法)并限制在给定时间运行的
N
个作业。我使用这种方法取得了很大的成功,但是基于您的需求和实施。
While CRON and Windows Scheduled Tasks are the tried and true ways of scheduling jobs/tasks to run on a regular basis, there are use cases where having a different scheduled task in CRON/Windows can become tedious. Namely when you want to let users schedule things to run, or for instances where you prefer simplicity/maintainability/portability/etc or all of the above.
In cases where I prefer to not use CRON/Windows for scheduled tasks, I build into the application a task scheduling system. This still requires 1 CRON job or Windows Task to be scheduled. The idea is to store Job details in the database (job name, job properties, last run time, run interval, anything else that is important for your implementation). You then schedule a "Master" job in CRON or Windows which handles running all of your other jobs for you. You'll need this master job to run at least as often as your shortest interval; if you want to be able to schedule jobs that run every minute the master job needs to run every minute.
You can then launch each scheduled job in the background from PHP with minimal effort (if you want). In memory constrained systems you can monitor memory usage or keep track of the PIDs (various methods) and limit to
N
jobs running at a given time.I've had a great deal of success with this method, YMMV however based on your needs and your implementation.
我认为你的观念需要改变。
PHP 无法调度作业,MySQL 也不能。 MySQL 中的触发器在 mysql 查询发生时执行,而不是在特定时间执行。 在 Web 开发中,这种限制通常不是
问题。 原因是您的 PHP 应用程序应该控制所有数据的进出。 通常,这仅意味着向用户或其他程序显示该数据或其他格式的 HTML。
对于你的情况,你可以这样思考。 截止日期是一个设定的日期。 您可以将其视为数据,并将其保存到数据库中。 截止日期何时发生并不重要,重要的是您在数据库中发送的数据是否被正确查看。
当向您的申请提出请求时,请检查截止日期是否已过去,如果是,则显示项目已关闭 - 或在显示之前更新项目已关闭。
确实没有理由独立于 PHP 应用程序来更新数据。
通常,您想要安排的唯一事情是会影响应用程序负载的作业,或者只需要完成一次的作业,或者并发或时间存在问题的作业。
就您而言,这些都不适用。
PS:我还没有尝试过 PHPscheduler,但我猜它不是一个真正的调度程序。 Cron 是一个守护进程,它会休眠直到给定任务在其队列中到期,然后执行该任务,然后休眠直到下一个任务到期(至少在当前算法中是这样做的)。 如果没有套接字和分叉扩展(作为特殊设置),PHP 就无法做到这一点。 因此,PHPscheduler 很可能只是在每次加载网页时(每当 PHP 执行页面时)检查任务的日期是否已过期。 这与您只是检查项目上的日期是否已过期没有什么不同,而无需 PHPScheduler 的开销。
I think your concept needs to change.
PHP cannot schedule a job, neither can MySQL. Triggers in MySQL execute when a mysql query occurs, not at a specific time. Neither
This limitation usually isn't a problem in web development. The reason is because your PHP application should control all data going in and out. Usually, this means just the HTML that displays that data, or other formats to users, or other programs.
In your case you can think about it this way. The deadline is a set date. You can treat it as data, and save it to your database. When the deadline occurs is not important, it is that the data you have sent in your database is viewed correctly.
When a request is made to your application, check if the date of the deadline is in the past, if it is, then display that the project is closed - or update that the project is closed, just before display.
There really is no reason to update data independantly of your PHP application.
Usually, the only things you want to schedule are jobs that would affect your application in terms of load, or that need to be done only once, or where concurrency or time is an issue.
In your case none of those apply.
PS: I haven't tried PHPscheduler but I can guess it isn't a true scheduler. Cron is a deamon that sleeps until a given task is due in its queue, executes the task, then sleeps till the next one is due (at least thats what it does in the current algorithm). PHP cannot do that without the sockets and fork extensions, as special setup. So PHPscheduler is most likely just checking if a date for a task has expired, on each load of a webpage (whenever PHP executes a page). This is no different then you just checking if the date on the project has expired, without the overhead of PHPScheduler.
对于任何与调度相关的事情,我总是会选择 cron 作业。
最大的好处是您也可以回显信息,并且它会通过电子邮件发送给您。
您会发现一旦开始使用 cronjobs,就很难停下来。
I would always go for a cron job for anything scheduling related.
The big bonus point is that you can echo info out as well and it get's emailed to you.
You'll find once you start using cronjobs, it's hard to stop.
PHPscheduler 怎么样..R 它们不比 cronjobs 更好吗? 我认为 crons 将独立于应用程序,因此如果必须更改主机会很困难..我不太确定..如果有人可以对此发表评论,那就太好了! 谢谢!
how about PHPscheduler..R they not better than cronjobs? I think crons would be independent of the application hence would be difficult if one has to change the host..i am not really sure though..It would be great if anyone can comment on this!! Thanks!