views:

51

answers:

4

I'm currently running an Apache server (2.2) on my local machine (Windows) which I'm using to run some PHP scripts to take care of some tedious work. One of the scripts involves a ton of moving, resizing, and download / uploading files to another server. I would very much like the script to run constantly so that I don't have to baby the script by starting it up again every time it times out.

set_time_limit(0); ignore_user_abort(1);

Both are set in my script, but after about 30mins to an hour the script stops and I get the 504 Gateway Time-out message in my browser. Is there something I missing in Apache or PHP to prevent the timeout? Or should I be running the script a different way?

+2  A: 

Or should I be running the script a different way?

Definitely. You should run your script from command line (CLI)

dev-null-dweller
Thanks for the answer! I was able to run it overnight without any problems this way. I can't thank you enough :D
tsp3
A: 

use phps system() to call a shell script which starts a service/background task

w13531
A: 

If you need to run it form your browser, You should make sure that there is not php execution limit in the php.ini file, but also that there is not limit set in mod_php (or what ever you are using) under apache.

Colum
A: 

if i should implement something like this i would you 2 different scripts:

  • A. process_controller.php
  • B. process.php

The workflow should be:

  1. the user call the script A by using a browser
  2. the script A start the script B by using a system() or exec() and pass to it a "process token" via command line.
  3. the script B write the execution status into a shared space: a file named as the token, a database table. in general something that can be read also by the script A by using the token as reference
  4. the script A contains an AJAX call, in polling, that ask to the script A the status of the process for a given token

Ajax polling:

<script>  
 var $myToken;  
 function ajaxPolling()
 {
   $.get('process_controller.php?action=getStatus&token='+$myToken, function(data) {
    $('.result').html(data);
 });

 }
 setInterval("ajaxPolling()",60*1000); //Every minute
</script>

there are some considerations about the communication between the 2 processes, depending on how many instances of the script B you would be able to run in parallel

  1. Just one: you don't need a random/unique token
  2. One per user: session_start(); $token = session_id();
  3. More than one per user: session_start(); $token = session_id().microtime();
Cesar
Awesome answer, thank you! If I ever need to run a huge script from a browser, I will definitely give your method a shot. Thanks! :)
tsp3