To use sleep() or cron job

cron, performance, php

Solution

Unless there's some cost savings in keeping the script running in memory, you're better off using cron to invoke it every hour, as needed.

0 0-2 * * * /usr/local/bin/mail-script.php

You can choose multiple hours using the - syntax, or the comma syntax:

0 0,1,2,3 * * * /usr/local/bin/mail-script.php

If it needs to maintain some form of state, use a temporary file to keep saved state.

Do:

> man 5 crontab

To see if your *nix handles the above cases.

Finally, unless you know the script has to run only 2-3 times, you're better off putting the logic about whether to "run or not to run" in the PHP script itself, and then just run it every hour.

Problem

I have this mail script I have to run a few times. To start the script I will use cron, but the script has to run 2 or 3 more times (with an hour apart). What's the best way to do this? To use the sleep command for an hour, or at the end of the script, place some code, so that the script will create a new cron job to run it self after an hour? Thanks

Original source