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, but found only one mention of a similar problem. The same issue arises if you try to execute the script from within a bash script and use exec to call sh, rather than calling PHP.
This issue does not arise when script 1 is called from the command line - under those circumstances, script 2 produces the expected output.
Any suggestions? I could use cron, but would prefer to be able to call script 2 when the user prompts it.