Monday, 15 March 2010

cron - Can a PHP script execute code after it returns data to the user? -



cron - Can a PHP script execute code after it returns data to the user? -

this question has reply here:

continue php execution after sending http response 12 answers

in case, want regularly check database expired data, , delete if has, i've added check start of php web responses. there's no need before user gets data, because it's not straight related them, , don't want slow downwards website.

i take in case should schedule cron task run php script, doesn’t seem portable solution. if migrate servers have manually add together cron task, , gets more complicated if changed platform.

so 2 questions are:

is possible homecoming info (either html page, or json info etc.), , then execute php code in order ensure fastest possible response time? if not, there portable way schedule php script?

you can utilize shell_exec (or similar functions) execute different php file (or script itself) in background.

here's example:

class="lang-php prettyprint-override">function exec_in_background($cmd) { if (substr(php_uname(), 0, 7) == "windows"){ pclose(popen("@start /b \"\" ". $cmd, "r")); } else { exec($cmd . " > /dev/null &"); } } if (isset($argv[1]) && $argv[1] == '--cron') { // database logic goes here } exec_in_background("php \"" . __file__ . "\" --cron");

you can add together top of script , should trick.

please note trying accomplish might not advisable if there many requests perform these actions many times.

php cron

No comments:

Post a Comment