Sometimes you might want to limit a script’s maximum execution time, so that if it’s not done in X seconds, you’ll discard any results you’ve got so far and move on. And if you’re lazy and don’t want to implement threading and semaphores and so on, or you just need a quick way to achieve this limitation and you’re okay with killing the script after a certain number of seconds – note that you will lose any unsaved data when you kill the program – then this Perl one liner might just do the trick for you:

perl -e '$s = shift; $SIG{ALRM} = sub { print STDERR "Timeout!\n"; kill INT => $p }; exec(@ARGV) unless $p = fork; alarm $s; waitpid $p, 0' {max. execution time in seconds} {/path/to/script}

Don’t try to read it…just go with it ;) It works and that all that matters.