Calling a PHP script from within PHP

software development


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, 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.


You can’t do a combination of both? Set Script 1 to set a flag to 1 or whatever in a database. Then run your cron every minute or whatever to check if that flag has been set. If it has, run Script 2.

I can see multiple reasons why this wouldn’t be working right, primarily among them that shell users and web ftp users would have different permissions. Allowing any web-facing script to run exec() seems like a substantial security risk.

Angela Gann
CrimsonDryad Web Design Services
Web Design, Custom Software Development