简体   繁体   English

如何在没有访问该页面的情况下每分钟自动运行php脚本?

[英]how to run php script automatically every minute without access to that page?

I'm developing website that will send email back to user automatically when they registered to my website. 我正在开发一个网站,当用户注册到我的网站时,它将自动向用户发送电子邮件。 I have searched from internet, most of them said that i have to used cron jobs; 我从互联网上搜索过,大多数人说我必须使用cron作业; the big problem foo me now is about cron jobs. 我现在面临的最大问题是关于cron的工作。 I don't how to write it and also how to execute it. 我不怎么写,也不怎么执行。 Can anyone gives me some example about it? 谁能给我一些例子吗?

Thank in advance. 预先感谢。

If we assume that the script that you want to run is located at /home/me/myscript.php then all you need to do is create a cron job that will run that script every minute. 如果我们假设要运行的脚本位于/home/me/myscript.php那么您要做的就是创建一个cron作业,该作业每分钟运行一次。

Several hosting companies have an interface (cPanel for instance) that will allow you to add a cron task easily. 几个托管公司有一个界面(例如cPanel),使您可以轻松添加cron任务。 You can also add the cron task by editing the relevant cron job and adding: 您还可以通过编辑相关的cron作业并添加以下内容来添加cron任务:

*/1 * * * * /usr/bin/php /home/me/myscript.php > /dev/null

If you are wanting to send a confirmation email to the user once they have registered, you can use php mail function to do this. 如果您希望在用户注册后向其发送确认电子邮件,则可以使用php邮件功能来执行此操作。

http://php.net/manual/en/function.mail.php http://php.net/manual/zh/function.mail.php

You can create a PHP file which has a heartbeat event. 您可以创建一个具有心跳事件的PHP文件。 You put that to sleep using the usleep function with the desired time interval and you can have a list of tasks to be executed (heartbeat tasks), knowing the time rules (when they should run, how often, and so on), and your heartbeat.php will check after each usleep which tasks should be executed and execute it. 您可以使用usleep函数以所需的时间间隔使其进入睡眠状态,并且可以列出要执行的任务(心跳任务),了解时间规则(应何时运行,何时运行等),以及您的heartbeat.php将在每次休眠后检查应执行哪些任务并执行它。 You can run this heartbeat.php on your server. 您可以在服务器上运行heartheart.php。

However, I do not see the reason why do you need a cron job, heartbeat or any periodic solution. 但是,我看不到为什么您需要进行cron工作,心跳或任何定期解决方案的原因。 Why don't you send the emails using php functions exactly when you need to send them? 为什么在需要发送电子邮件时不完全使用php函数发送电子邮件?

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

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