Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute PHP script in background... what is the best solutions for my case?

I am working on a emailer system that I have to plug in a CMS for a company. I am looking for a way to make the email sender script run in background while the admin may navigate or even close the browsers.

I found the PHP function named ignore_user_abort() which is required to keep the page running even if it has timed-out.

Now I have three solutions to "start the script" :

  1. I could use an iframe

  2. I could use an Ajax call which I have previously configured to timeout very early. Eg: using the jQuery framework : $.ajaxSetup({ timeout: 1000 });

  3. I could use a cron job but I would like to avoid this solution since they are "virtual" and are unstable on that server.

Is there any other solutions? I dont really like the iframe solution but I already use them with an Ajax uploader script.

I dont want the admin to hit F5 and start a second instance of it.

The company's users have been told to only log in the CMS using Firefox. Thank you

like image 524
Cybrix Avatar asked Dec 22 '22 21:12

Cybrix


1 Answers

You can run a PHP script in the background using Exec().

php docs provide an example on how you can do that:

function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
}
like image 76
Jan Hančič Avatar answered Dec 24 '22 11:12

Jan Hančič