ITeye博客

Time out script in perl

Ashton · 2008-03-13 13:06 · Markdown

I have a script used to test the sftp connection which runs too long, so I want to time it out after a given number of seconds.After a google search and perl doc reading, I find the ALARM function can work well.

#!/usr/bin/perl eval { local %SIG;//in some ENV %SIG should be global variable $SIG{ALRM}= sub{ die "timeout reached, after 20 seconds!\n"; }; alarm 20; print "sleeping for 60 seconds\n"; sleep 60; # This is where to put your code, between the alarms alarm 0; };

alarm 0;

if($@) { print "Error: $@\n"; }

exit(0);

reference:http://perl.coding-school.com/perl-timeout/

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。