如何通过php脚本设置cron?
我有 10 个用户。我想为这 10 个用户设置一个 cron。现在,当特定用户登录时,他可以从面板设置玉米时间,并且 cron 将在那时运行。此外,当新用户添加时,将根据应用程序为该用户设置默认时间的新 cron。
我怎样才能做到这一点?
i have 10 users. and i want to set a cron for that 10 users. Now when a perticular user logged in he can set the corn time from his panel and cron will run at that time .Also when new user added a new cron will be set for that user with default time according to application.
how can i achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我同意 thomasmalt 的观点,这并不明智,但你应该做的是拥有它,这样你的用户就可以在“用户”数据库表(如 mysql)中设置 cron 时间,其中包含以下列:“间隔(INT)”和“最后更新(日期时间)'。所以每个用户都有自己的时间设置。然后您决定允许的最低增量是多少(例如 1 分钟),并且只允许您的用户设置高于该增量的时间。
然后每 1 分钟运行一次 cron 来检查“用户”表。查找间隔小于或等于“last_update”以来时间的任何用户。然后为这些用户运行您的操作。这实际上与为每个用户设置 crons 执行相同的操作。
I agree with thomasmalt that it is not wise but what you should do instead is have it so your users can set a cron time in a 'users' database table (like mysql) with columns: 'interval (INT)' and 'last_update (DATETIME)'. So every user has their own time set. Then you decide what is the lowest increment you will allow (such as 1 minute) and only let your users set times higher than that.
Then you run a cron every 1 minute which checks the 'users' table. Find any users where interval is less than or equal to the time since 'last_update'. Then run your actions for those users. This effectively does the same thing as setting crons for every user.
这听起来通常是一件非常不明智的事情,但是如果您知道自己在做什么并了解所涉及的安全问题等。我建议使用两个脚本来解决这个问题,
向 cron 添加内容可能是一件非常危险的事情,只有非常受信任的用户才可以这样做。任何网络解决方案都应被视为不安全,您应该格外小心以确保任何输入都经过正确审核。
This sounds generally like a very unwise thing to do, but if you know what you're doing and understand the security problems involved etc. I would suggest solving this using two scripts
Adding stuff to cron is potentially a very dangerous thing and only very trusted users should be allowed to do so. Any web solution should be regarded as insecure and you should take extra care to make sure any input is properly audited.
正如 thomasmalt 所说,在公共系统中这样做可能并不明智……您不想让用户弄乱系统 crons。
我建议您让用户在控制面板中指定一天中的某个时间,并每分钟运行一个 cron 脚本。在该脚本中,很容易获取与当前时间匹配的用户并执行他们的脚本。它实现了同样的目标,只是更冗长一些,需要编写更多代码,但更安全。
As thomasmalt said this is probably not wise to do in a public system ... you don't want to let users mess with system crons.
I suggest you let the users specify a time of day in their controlpanel and have a cron script run every minute. In that script it's easy enough to fetch the users that match the current time and execute their script. It achieves the same thing, is a little more verbose and more to code but alot more secure.