从 php 创建 crons
我想在 PHP 中创建 cron 作业,有点像在 cPanel 或 Plesk 中,但我陷入困境。我想以编程方式执行此操作,而不是强迫用户转到 cPanel 手动创建 cron...
尝试了很多东西,阅读了很多东西...包括 stackoverflow 上的这里,没有任何效果。 我尝试从 php 编辑 crontab,例如 shell_exec('crontab /pathtomycronfile/cron.txt'),然后编辑 cron 文件本身,但它不起作用。用户是“apache”,我尝试为 apache 设置 crontab ..也不起作用...
请有人帮忙解决这个问题吗?您以前能够从 php 创建 cron 作业吗?
I want to create cron jobs from within PHP, a bit like in cPanel or Plesk but I'm stuck. I want to do this programatically and not force the user to go to cPanel to create the crons manually.....
Tried many things, read many things... including here on stackoverflow, nothing works.
I try editing crontab from php like shell_exec('crontab /pathtomycronfile/cron.txt') and then editing the cron file itself but it doesn't work. User is 'apache' and I tried setting the crontab for apache .. doesnt't work either ...
Please can anyone help with this? Have you been able to create cron jobs from php before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在某些 *nix 系统下, crontab 会将每个用户特定的 cron 文件存储在 /var/spool/cron/crontabs/[USERNAME] 中。它们不适合直接编辑,但您可以使用 PHP 来进行编辑。
您可能还需要更改包含目录的权限,以便 PHP 可以看到该文件并对其进行编辑。然后你可以这样做,例如:
不过,老实说,这是非常危险的。相反,您应该考虑拥有一个 cron.php,它每 15 分钟调用一次,并将有关在给定一刻钟内应执行哪些实际任务的所有逻辑放入该 cron.php 中。不要编辑 crontab 以将逻辑放入其中。
Under some *nix systems, crontab will store each user-specific cron file in e.g. /var/spool/cron/crontabs/[USERNAME] . They're not intended to be edited directly, but you could get PHP to do so.
You might also need to change the permissions on the containing directory, so that PHP can see the file to edit it. You could then do so with e.g:
To be honest, though, this is very dangerous. Instead, you should consider having a cron.php, which gets called every 15 minutes, and put all your logic about what actual tasks should be carried out at a given quarter-hour in that cron.php. Don't edit the crontab to put the logic in there.
Cron 作业通常需要从主机提供商的控制面板创建。
我认为没有办法从程序中做到这一点。
Cron jobs normally needs to be created from Controlpanel from the host provider.
I dont think there is a way to do from program.