I used to use exec()
function to trigger new php threads (And don't need the output of these php scripts), something like
for ($i=0;$i<5;$i++) {
exec('/usr/bin/php script' . $i . '.php > /dev/null 2>&1 &');
sleep(1);
}
I'd like to ask if there is any alternative native PHP function to do the same job, since my web hosting provider does not enable exec
and i'm not willing to switch to another one.
Thank you very much.
Typically such tasks are spawned via internal requests inside the http server. Just use phps [virtual()][1]
or the [curl extension][2]
for more complex tasks.
You are however limited to phps web configuration, so take care about the limits like execution timeout and the like. I doubt there is a way to spawn a real process on a web hosters system. That would be a horrible security thread!
Often such tasks are also delegated to some sort of job scheduler implementation which is triggered by cron
, poor mans cron
or an ajax
based trigger. This means you have to implement a scheduler for jobs waiting to be processed. But it also brings a good separation of separate jobs.