Time out script in perl

本文介绍了一种使用Perl语言实现的定时超时处理方法。通过设置ALARM信号处理函数,在指定的时间后(例如20秒),如果脚本仍在运行,则会触发超时并退出。这种方法适用于长时间运行的脚本,如SFTP连接测试等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值