简体   繁体   中英

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; 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?

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.

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:

*/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.

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

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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