gpt4 book ai didi

linux - 如何防止我的 Perl 脚本中的错误终止脚本?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:42:44 25 4
gpt4 key购买 nike

我有一个简单的 Perl 脚本,它使用无限循环作为 Linux 守护进程运行。它每 10 秒连接一次数据库以执行一个进程。

while (1)
{
# THIS LINE WILL KILL THE SCRIPT IF IT FAILS
my $DB=DBI->connect("dbi:Sybase:server=myserver","user","password");
. . . do something . . .
sleep (10);
}

我有两个问题:

  • 如果数据库不可用,我该如何保持脚本运行?
  • 我可以添加异常处理程序来向我发送电子邮件或记录错误吗?

最佳答案

这尝试以 10 秒的间隔连接,而不是每 10 秒,正如 William Pursell 指出的那样:

while (1)
{
# THIS LINE WILL KILL THE SCRIPT IF IT FAILS
my $DB;
eval {
$DB = DBI->connect("dbi:Sybase:server=myserver","user","password");
};
if ( my $ex = $@ ) {
warn $ex;
next;
}
# do something with $DB
continue {
sleep 10;
}
}

另见 Object Oriented Exception Handling in Perl, is it worth it?How can I cleanly handle error checking in Perl?

关于linux - 如何防止我的 Perl 脚本中的错误终止脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1281835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com