3

I am running a php script as a cron job that might take very long time to finish. It will create a massive xml file and save it.

What should I think of if I implement it?

One thing I do is set max_execution_time for a long time:

ini_set('max_execution_time', 300);

Is there anything else I should do? Increase memory limit? Does it help if put header "keep-alive"?

What can I do to make sure the script will always run until everything neccessary is done?

  • Wat are you trying to run? – Mazzy Oct 29 '13 at 22:33
  • I import huge amount of data from another server using their API and save it all as xml file. –  Oct 29 '13 at 22:38
  • php might not be what you're looking for, php is pretty slow processing large ammounts of data compared to other languages – Mazzy Oct 29 '13 at 22:41
  • 1
    Absolutely, but in this case using PHP is unfortunately the only possibility for me. –  Oct 29 '13 at 22:44

1 Answers1

5

You can remove the execution time limit by using the set_time_limit function passing 0 as parameter:

set_time_limit(0);

Adding HTTP headers won't help because as it is a cronjob script, you are not dealing with a browser.

Guilherme Sehn
  • 6,727
  • 18
  • 35
  • Is there any difference between max_execution_time and set_time_limit? –  Oct 29 '13 at 22:38
  • 1
    If you do not call `set_time_limit` PHP will use the `max_execution_time` value from php.ini to define it. Both `ini_set('max_execution_time', ...)` and `set_time_limit(...)` will set this value only for the current request, so I think there is no practical difference between them. – Guilherme Sehn Oct 29 '13 at 22:42
  • I understand, so all in all it should be enough to make sure the script running as a cron job will get its job done? –  Oct 29 '13 at 22:46
  • Well, if your script is very complex (or poorly optimized) there is the possibility of it reach the memory limit, but it's hard to measure this. Make some tests with real data and if it reach, you can increase it by setting the `memory_limit` configuration via `ini_set`. – Guilherme Sehn Oct 30 '13 at 10:35