Looping a PHP Script

cron, infinite-loop, php

Solution

You could run a parent php process that forks a client at an interval. If you're curious about exploring it as an option here is a good starting point: https://www.php.net/pcntl Nice thing about doing it this way is that the parent process can kill client pids that do not end within a reasonable amount of time.

If you're looking for something quick and dirty you could write a bash script to invoke the php quite easily (if you're on linux):

#!/bin/bash
while [ "true" ]; do
        /path/to/script.php
        sleep 15
done

EDIT You don't really even need the script, bash will do it all on one line:

while [ "true" ]; do /path/to/script.php; sleep 15; done

Problem

I've got a PHP script that checks a directory and deletes any files not modified within 15 seconds (It's for a game). My problem is how to get this script to run all the time. I set up a cron job to run every 10 minutes and then in the PHP script I have an infinite loop with a sleep(10). My thought was that it would run the code every 10 seconds, and in the case the script stopped, the cron job would restart it eventually. However, after the script is started, it runs for about 3 loops (30 secs) and then stops. I've heard PHP only gets so much memory per file load. How can I make this PHP script loop indefinitely? Maybe there is some way to call itself

Original source