如何通过php脚本设置cron?

发布于 2024-09-25 09:17:46 字数 134 浏览 0 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(3

北渚 2024-10-02 09:17:46

我同意 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.

殤城〤 2024-10-02 09:17:46

这听起来通常是一件非常不明智的事情,但是如果您知道自己在做什么并了解所涉及的安全问题等。我建议使用两个脚本来解决这个问题,

  • 其中一个 php 部分可以让用户查看他的 crontab 并操作时间设置使用 html 表单并将所需的更改存储在系统上的已知位置。
  • 一个 shell 脚本部分作为系统上受信任的用户运行,以给定的时间间隔(可能一分钟一次)获取更改并对 cron 进行实际更新。

向 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

  • one php part which lets the user view his crontab and manipulate time settings using an html form and storing the wanted changes in a known place on your system.
  • one shell script part running as a trusted user on your system picking up the changes at a given interval (once a minute maybe) and doing the actual updates to cron.

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.

就是爱搞怪 2024-10-02 09:17:46

正如 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.

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