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

时间:2021-12-17 04:45:53

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; the big problem foo me now is about cron jobs. I don't how to write it and also how to execute it. Can anyone gives me some example about it?

我正在开发网站,当他们注册到我的网站时会自动向用户发送电子邮件。我从互联网上搜索过,大多数人说我必须使用cron工作;我现在最大的问题是关于cron的工作。我不知道怎么写它以及如何执行它。谁能给我一些关于它的例子?

Thank in advance.

预先感谢。

3 个解决方案

#1


2  

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. You can also add the cron task by editing the relevant cron job and adding:

一些托管公司有一个接口(例如cPanel),可以让你轻松添加一个cron任务。您还可以通过编辑相关的cron作业并添加以下内容来添加cron任务:

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

#2


1  

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 mail功能执行此操作。

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

#3


0  

You can create a PHP file which has a heartbeat event. 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. You can run this heartbeat.php on your server.

您可以创建具有心跳事件的PHP文件。您可以使用具有所需时间间隔的usleep功能将其置于睡眠状态,并且您可以拥有要执行的任务列表(心跳任务),了解时间规则(运行时间,运行频率等),以及heartbeat.php将在每次睡眠后检查应执行哪些任务并执行它。您可以在服务器上运行此heartbeat.php。

However, I do not see the reason why do you need a cron job, heartbeat or any periodic solution. Why don't you send the emails using php functions exactly when you need to send them?

但是,我没有看到你需要cron工作,心跳或任何定期解决方案的原因。为什么不在需要发送时使用php函数发送电子邮件呢?

#1


2  

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. You can also add the cron task by editing the relevant cron job and adding:

一些托管公司有一个接口(例如cPanel),可以让你轻松添加一个cron任务。您还可以通过编辑相关的cron作业并添加以下内容来添加cron任务:

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

#2


1  

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 mail功能执行此操作。

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

#3


0  

You can create a PHP file which has a heartbeat event. 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. You can run this heartbeat.php on your server.

您可以创建具有心跳事件的PHP文件。您可以使用具有所需时间间隔的usleep功能将其置于睡眠状态,并且您可以拥有要执行的任务列表(心跳任务),了解时间规则(运行时间,运行频率等),以及heartbeat.php将在每次睡眠后检查应执行哪些任务并执行它。您可以在服务器上运行此heartbeat.php。

However, I do not see the reason why do you need a cron job, heartbeat or any periodic solution. Why don't you send the emails using php functions exactly when you need to send them?

但是,我没有看到你需要cron工作,心跳或任何定期解决方案的原因。为什么不在需要发送时使用php函数发送电子邮件呢?