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.
Write once. And that’s about it… Never try to read it after you’ve found that it works. Perl™.
I didn’t try to read it even before I’ve found that it works
That’s the Perl attitude. We shouldn’t bash Perl, or should we?!
great thinking huh?
luckily it does work on my end. Thanks.
Thanks for telling us not to read it, that was the key to making it work!