Calling a PHP script from within PHP
Posted by: ntm
Posted on: 2007-07-09 05:17:00
Hi -
I'm trying to execute PHP script 2 from within PHP script 1 - the idea is that script 2 will carry on processing data until it has finished doing so, even if the user logs off or browses to a different part of the website.
The main problem I am encountering is that if you do the following (the 'bash -c etc.' part serves to background the process and return control to PHP) -
exec('bash -c "exec nohup setsid /usr/local/bin/php /path/to/php/script/2.php > /path/to/output/file.txt 2>&1 &"');
- then file.txt will end up containing the output of script 1 - NOT script 2. I have tried to find some information on this, and the only post I could find addessing the issue mentioned that calling PHP from within PHP does not seem to be possible. The same happens if you try to execute the script from within a bash script and use exec to call sh, rather than calling PHP.
Any suggestions? I could use cron, but would prefer to be able to call script 2 when the user prompts it.