Skip to content

Instantly share code, notes, and snippets.

@tentacode
Last active August 29, 2015 14:19
Show Gist options
  • Save tentacode/22c8da5eb0ea6891f4e2 to your computer and use it in GitHub Desktop.
Save tentacode/22c8da5eb0ea6891f4e2 to your computer and use it in GitHub Desktop.
Quick & dirty loop with pcntl_fork
<?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();
@tentacode
Copy link
Author

For the record this has been used in a preprod environment to simulate a crontab without having to actually do a crontab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment