How to run php code in background on unix server

On my website, I have the function of loading data feeds by users. These data channels typically have over 30,000 records. And each entry must go through a complex verification process. Thus, to complete this task, it will take more than 1 hour to process the chips. This long-term process is inevitable.

Here these channels are uploaded by a public user. File size less than 5 MB text file. Therefore, we do not want to provide FTP access, nor do we want to provide command line access to these users. Only we prefer to provide web access for downloading feeds. And we must process the feed immediately after they are downloaded. And after processing, we also need to send them a mail report.

Now the problem is that web pages with long jobs throw some proxy server problem into many network jobs (since it takes 1 hour to answer). This is a problem with most client network configurations.

I need a way to run a PHP script after loading the feed. And that the script should work in the background, at the same time, correctly complete the page loading without delaying the client.

I will make a background for processing data and sending mail to the client after the process is completed. The Cron tab is not available here, as the client can only use this feature several times a month and in an unforeseen time range.

If there is a way to run a PHP script in the background, but through web access, let me know.

Sample script to simplify the requirement

Assume this example and help me modify this script to achieve the result. I have two PHP scripts "thread.php and createfile.php". A file called "thread.php" will be opened by the client through a web browser. This script should execute the script "createfile.php". But the answer will take 20 seconds. But we want it to work in the background. And print immediately in the browser.

Below is the code for the sample scripts.

createfile.php ( http://sugunan.net/demo/createfile.php )

<?php sleep(20); @unlink("phpfile.txt"); $myfile = fopen("phpfile.txt", "w") or die("Unable to open file!"); $txt = date("h:i:s")."\n"; fwrite($myfile, $txt); fclose($myfile); mail("someone@example.com","Background process","Process completed"); ?> 

the created file is displayed on the web url: http://sugunan.net/demo/phpfile.txt

thread.php ( http://sugunan.net/demo/thread.php )

 <pre> <?php echo "Script start at: " . date('h:i:s') . "\n"; include("createfile.php"); //give a method to execute this script but in background without delay echo "Script end at: " . date('h:i:s'); ?> 

This will give the following result with a delay to the sleep() function.

Script start at: 02:53: 27

Script end at: 02:53: 47

Is there any way to execute "createfile.php" without the include() method. And to avoid a 20 second sleep delay while processing "createfile.php" in the background?

Please think that I want to avoid client side latency and want to send it immediately to the browser.

+3
unix php
Sep 06 '14 at 13:41
source share
3 answers

Finally, I got a working answer from the following message: php exec command (or similar) so as not to wait for the result

So, I edit the code as follows to make my requirement work. But I do not know how this works. I just copy the syntax from this answer and went through it here with a modification.

 <?php echo "Script start at: " . date('h:i:s') . "\n"; exec('bash -c "exec nohup php createfile.php > /dev/null 2>&1 &"'); echo "Script end at: " . date('h:i:s'); ?> 

Here php createfile.php will be a command to execute a PHP script and continue without waiting for output.

Thanks Cristian

0
Sep 06 '14 at 18:10
source share

Use exec ()

http://php.net/manual/en/function.exec.php

This will allow you to run a lengthy process on another thread. I used this for all other PHP, Python or C applications to convert wav to mp3, convert video or parse through large XML files ...

Be sure to inform the user or this, possibly by email, when it is completed, and if it was successful.

EDITED with working code in purehuman.info/demo ... it actually writes the file now. Here is what I would like to do thread.php as follows:

 <?php echo "Script start at: " . date('h:i:s') . "\n"; exec("php -f createfile.php /dev/null &"); echo "Script end at: " . date('h:i:s'); ?> 

If you are in the windows:

 <?php echo "Script start at: " . date('h:i:s') . "\n"; exec("start /B php createfile.php"); //obviously change the paths etc where needed.. echo "Script end at: " . date('h:i:s'); ?> 

you need to pay tribute to the parts of the window: How to execute PHP scripts in the background using EXEC () and CMD

+1
Sep 06 '14 at 13:56 on
source share

PHP is not intended for this kind of work. But there are tricks to try to do it.

If your feeds have more than 30,000 entries, you will have timeout issues with PHP FPM. Thus, your script should work with the PHP CLI (check your server settings for the CLI timeout for PHP).

You should look at Rabbit MQ and program the cron job every minute to consume messages in the queue. Here is an example from PHP http://www.rabbitmq.com/tutorials/tutorial-two-php.html

Hope this helps you

0
Sep 06 '14 at 14:20
source share



All Articles