Issue
Is using exec
to run nohup from a php script the right way to go if a long running action needs to run but the web request needs to return right away? I want to spawn another process from a php script that runs after the request is made.
Example:
exec ('nohup php long_running_script.php');
//Need this to return happen right away without waiting for long_running_script.php
echo json_encode(array('success' => TRUE));
Solution
If at command is available on the hosting, I prefer to use:
putenv( 'SHELL=/bin/sh' );
print `echo /usr/bin/php -q full_path_to_script.php | /usr/bin/at now 2>&1`;
I would check every command's path to be sure that everything is executed correctly:
echo exec( 'which at' );
Answered By - Mat