However, I've also found suggestions that sleep
might slow down the
server.
Yes, and hitting the pause
button on a movie playing on your computer will slow down the duration of the film based on the amount of time you pause
the movie.
The purpose of sleep
is to put a pause in your script. As described in the official PHP documentation:
Delays the program execution for the given number of seconds.
So yes, it slows down your server. But only on content or pages where sleep
is active.
So if this is a fronted script with sleep
in it, it slows down the ability for anyone to view content via the PHP script that uses sleep
. Place it in the middle of a page where HTML is rendering with a 1 second delay & your page now takes 1 second longer to render.
If this is a backend process only you really know about or trigger, no big deal. It’s a background process anyway so it will just expectedly slow things down in that realm.
That said, let’s look at your core question which is the first sentence of your post:
I’m looking at ways to send out 1 email every 1 min.
Then what you are looking for is a cron
job which is a timed job on a Unix/Linux system. An entry for a cron
job for something sending mails out every minute might be something like this:
* * * * * /full/path/to/php /full/path/to/your/php/script.php
But that is superficial. It basically just triggers the script.php
every minute. Then within your script.php
you would have to create some core logic that would control what happens each time it’s triggered. If you are using a database, then maybe you could create a last_sent
field where you sent a time stamp of the last time a mail was sent to a recipient and then you act on that. But again, the logic is based on your core needs.
But at the end of the day, I am not too clear how sleep
would factor into any of this. Might be worth it to take a step back and better architect your script to fit your needs knowing what cron
is, what sleep
is & what they are as well are not.