I have a php script I need to run every 5 seconds (run, wait until it’s done, wait 5 seconds, run again)
I have two ways of doing it. either have an infinte loop in the script with a sleep function that would look like that:
while (1)
{
do_stuff();
sleep 5;
}
or have bash script that will run the script and wait for 5 seconds. It would look like that
while [ 1 ]; do
php script.php &
wait
sleep 5
done
I was wondering what is the most efficient way to do it.
The php script i am running is a codeigniter controller that does a lot of database calls..
If you’re doing a lot of DB calls, then do the sleep within php. That way you’re not paying the php startup penalty, the connect-to-the-db penalty, etc… that’l be incurred if you’re sleeping in bash.
When you do the bash loop, you’ll be starting/running/exiting your script each iteration, and that overhead adds up quickly on long-running scripts.
On the other hand, at least you’ll start with a “clean” environment each time, and won’t have to worry about memory leaks within your script. You may want to combine both, so that you loop/sleep within PHP (say) 100 times, then exit and loop around in Bash.