Lock
Lock

Reputation: 5522

php- set a script timeout and call a function to cleanup when time limit reached

I am writing a PHP script that will do some processing, but I only want it to run if the script isn't already running. I also only want the script to run for a maximum of 5 minutes.

I know I can set the timeout using set_time_limit, but doing so will not remove the lock files that I create during the execution.

Is their a way to call a function when the time limit is reached so I can perform a cleanup?

Upvotes: 2

Views: 1320

Answers (1)

Steven Moseley
Steven Moseley

Reputation: 16325

Tried this out in a test environment and got it working with the following (error reporting off + shutdown function):

<?php

$test = function() {
    print("\n\nOOPS!\n\n");
};

error_reporting(0);
set_time_limit(1);
register_shutdown_function($test);

while (true) {
}

Upvotes: 2

Related Questions