PHP background processes

I am trying to create a PHP script, I have finished the script, but it only takes 10 minutes to complete the process that it is intended for. This is not a problem, however I believe that I have to constantly load the page, which is annoying. Can I use it to start the process and then go back after 10 minutes and just view the log file that it generated?

+41
php background-process
Nov 05 '08 at 13:00
source share
10 answers

Well, you can use ignore_user_abort (true) "

So, the script will continue to work (keep track of the duration of the script, maybe add " set_time_limit (0)")

But the warning is here: you cannot stop the script with these two lines:

ignore_user_abort(true); set_time_limit(0); 

In addition, you can directly access the server and kill the process there! (He was there, made an endless cycle, calling himself over and over again, forcing the server to enter the stop screech, shouting at ...)

+49
Nov 05 '08 at 13:09
source share
β€” -

It looks like you should have a queue and an external script to handle the queue.

For example, your PHP script should put a record in a database table and return immediately. Then, cron, running every minute, checks the queue and expands the process for each job.

The advantage is that you do not block the apache thread for 10 minutes.

+26
Nov 05 '08 at 16:37
source share

I had a lot of problems with a similar process under the windows; My situation was slightly different from the fact that I didn’t care about the β€œscript” reaction - I wanted the script to run and allow other page requests to go through while it was busy with work.

For some reason; I had problems with it, either hanging other requests, or the time runs out after 60 seconds (both apache and php were set to timeout after about 20 minutes); It also turns out that firefox shuts down after 5 minutes (by default), so after this point you cannot know what is going on through the browser without changing the settings in firefox.

I ended up using an open process and processed closing methods to open php in cli mode as follows:

pclose(popen("start php myscript.php", "r"));

This (using start) will open the php process and then kill the startup process by running php so that it works for a long time - again you would need to kill the process to manually close it. You did not need to set any timeouts, and you could let the current page that called it continue and provide some details.

The only problem is that if you need to send the script any data, you will either do it through another source, or pass it on the "command line" as parameters; which is not so safe.

Works well for what we need, and ensures that the script always runs and can run without any interruptions.

+8
Nov 05 '08 at 14:13
source share

It may be useful as well: Asynchronous exec shell in PHP

+6
Nov 05 '08 at 14:02
source share

I think the shell_exec command is what you are looking for.

However, it turns off in safe mode.

A PHP help article about this is here: http://php.net/shell_exec

There is an article about it here: http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/

+5
Nov 05 '08 at 13:08
source share

There is another option that you can use, run the CLI script ... It will run in the background, and you can even run it as a cronjob if you want.

eg

 > #!/usr/bin/php -q <?php //process logs ?> 

This can be configured as a cronjob and will run without a time limit .... these examples relate to a unix-based operating system.

FYI I have a php script working with an infinite loop that does some processing and has been working for the last 3 months without stopping.

+4
Nov 06 '08 at 7:27
source share

You can use ignore_user_abort() - this way the script will continue to work even if you close your browser or go to another page.

+3
Nov 05 '08 at 13:09
source share

Think About Gearman

Gearman is a general application framework for running multiple machines or processes. This allows applications to complete tasks in parallel, to handle load balancing and to call functions between languages. The structure can be used in a variety of applications, from high availability websites to transport database replication events.

This extension provides writing classes for Gearman customers and employees. - Source php guide

Gearman Official Website

+2
Jun 25 '13 at 14:32
source share

In addition to the bastiandoeen answer, you can combine ignore_user_abort(true); with cUrl request .

Fake a request abortion by setting low CURLOPT_TIMEOUT_MS and continue processing after closing the connection:

 function async_curl($background_process=''){ //-------------get curl contents---------------- $ch = curl_init($background_process); curl_setopt_array($ch, array( CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER =>true, CURLOPT_NOSIGNAL => 1, //to timeout immediately if the value is < 1000 ms CURLOPT_TIMEOUT_MS => 50, //The maximum number of mseconds to allow cURL functions to execute CURLOPT_VERBOSE => 1, CURLOPT_HEADER => 1 )); $out = curl_exec($ch); //-------------parse curl contents---------------- //$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //$header = substr($out, 0, $header_size); //$body = substr($out, $header_size); curl_close($ch); return true; } async_curl('http://example.com/background_process_1.php'); 

NB

If you want a cURL timeout in less than one second, you can use CURLOPT_TIMEOUT_MS, although there is a bug / "feature" on "Unix-like systems", which leads to the immediate exit of libcurl if the value is <1000 ms with an error "cURL Error (28): timeout has been reached." Explanation of this behavior:

[...]

The solution is to turn off signals using CURLOPT_NOSIGNAL

pros

  • No need to switch methods (compatible windows and Linux)
  • No need to implement connection processing through headers and buffer (regardless of browser version and PHP)

against

  • Curl extension required

Resources

+2
Feb 21 '15 at 13:11
source share

Zuk.

I am sure this will work:

 <?php pclose(popen('php /path/to/file/server.php &')); echo "Server started. [OK]"; ?> 

"&" it is important. It tells the shell not to wait for the process to complete.

You can also use this code in your php (as "bastiandoeen" said)

 ignore_user_abort(true); set_time_limit(0); 

in the server stop command:

 <?php $output; exec('ps aux | grep -ie /path/to/file/server.php | awk \'{print $2}\' | xargs kill -9 ', $output); echo "Server stopped. [OK]"; ?> 
+1
Jan 11 '14 at
source share



All Articles