Custom module stopped executing after 30 seconds
You need write batch operation:


  Functions allowing forms processing to be spread out over several page
  requests, thus ensuring that the processing does not get interrupted
  because of a PHP timeout, while allowing the user to receive feedback
  on the progress of the ongoing operations.


http://api.drupal.org/api/drupal/includes%21form.inc/group/batch/7