in reply to Script dieing after arbitrary time

This appears to be pseudocode rather than a working example of your problem. While you display the strict pragma you use many variables without my.
Maybe taking a look at: Before you Post... and maybe How (Not) To Ask A Question