It might make sense for you to have a parent process that forks off a child process to do the work. Then it can catch SIGCHLD signals when things go wrong. Check out the
IPC::Run for a convenient wrapper for this, including a time-out option for the child process.