The script will terminate processing when it runs over its time limit. If you want to do something more after that...
If the script is running slow because you've got a loop which runs for a large number of iterations: record the time at the start of the loop; first thing you do once you're in the loop is look at the current time and compare it with the recorded time; more than 20 seconds difference, break and do whatever it is you do to wrap up; otherwise go through the loop.
If it's running slow because PHP is waiting for some external resource - or some other situation where the loop is not under your control - then the abort can either be handled by the external resource or you can break the slow job down into small pieces you can deal with in a loop (reducing the case to the one above).
Or (something I haven't tried): adjust the time limit as suggested earlier, and use [man]register_shutdown_function[/man] just before embarking on the big job. I don't know if this will work however - i.e., whether PHP will run it because the script "timed out". See also [man]register_tick_function[/man] which may also be useful.