Last active
August 29, 2015 14:19
-
-
Save tentacode/22c8da5eb0ea6891f4e2 to your computer and use it in GitHub Desktop.
Quick & dirty loop with pcntl_fork
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// every thing before will be used in every child process | |
while(true) { | |
$pid = pcntl_fork(); | |
if ($pid === -1) { | |
die('Could not fork process'); | |
} elseif ($pid) { | |
// parent wait for child to end | |
// only the parent will keep staying in the loop | |
// forking again after 5 minutes when previous child ended | |
pcntl_wait($status); | |
sleep(5 * 60); | |
} else { | |
// child gets out of the while loop to execute next code and terminate after | |
// child will never loop nor fork | |
break; | |
} | |
} | |
// following script will repeat itself after 5 minutes | |
doSomething(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the record this has been used in a preprod environment to simulate a crontab without having to actually do a crontab