简体   繁体   English

如何使用PHP在我的网站中设置cron作业?

[英]How can I set up cron job in my site using PHP?

I want to mail after 6 hours automatically to my user who hasn't fully completed form on my website. 我想在6个小时后自动邮寄给尚未在我的网站上完整填写表格的用户。

Help Me 帮我

Use crontab -e to edit the cron table for your account. 使用crontab -e编辑您帐户的cron表。

In the crontab, put an entry something like... 在crontab中,输入类似...的条目

0,10,20,30,40,50 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php

or the equivalent 或同等学历

*/10 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php

...which will run the cron handler php file every 10 minutes using wget (there are other options as well, and you may need to edit the command appropriately). ...这将使用wget每10分钟运行一次cron处理程序php文件(还有其他选项,您可能需要适当地编辑命令)。 (Note: you don't want to just run it every 6 hours, because then if someone happened to fill out the form right after it ran, it wouldn't have been 6 hours since they filled it out next time it runs, so you'd end up with 10-11 hour gaps.) (请注意:您不想只每6小时运行一次,因为如果有人碰巧在表单运行后立即填写该表单,则距离他们下次运行该表单就不会过去6个小时,因此您最终将有10-11小时的时间间隔。)

Then in your PHP file, find users who BOTH (a) haven't fully completed the form for at least 6 hours and (b) haven't been emailed yet. 然后在您的PHP文件中,找到以下两个用户:(a)至少有6个小时没有完全填写表格,并且(b)尚未通过电子邮件发送。 Send them an email, and mark them as having been emailed. 向他们发送电子邮件,并将其标记为已通过电子邮件发送。

You will need to create the php script that does the checking and mailing, and then set the cron job like the following 您将需要创建执行检查和邮寄的php脚本,然后像下面这样设置cron作业

/path/to/php -q /home/username/public_html/mycheckingscript.php

Obviously you will need to adjust the first path to point to your php binary, and the second path to point to the full location of your checking & mailing script. 显然,您将需要调整第一个路径以指向您的php二进制文件,第二个路径以指向您的检查和邮件脚本的完整位置。

I don't think you want to set the cron up using php. 我不认为您想使用php设置cron。 Instead write a php script and then have cron execute that script every hour or so. 而是编写一个php脚本,然后让cron每隔一个小时左右执行一次该脚本。 This would be something that is going to be dependent on your operating system. 这将取决于您的操作系统。

For linux, here is the manpage for using crontab. 对于linux,这是使用crontab的手册

There is no way you can change/add a schedule on the cron job on the fly. 您无法即时更改/添加cron作业的时间表。 according to my experience. 根据我的经验。 because until now i did not find.. 因为直到现在我都没有找到..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM