gpt4 book ai didi

linux - Perl 信号处理会截断与数据库的连接并导致错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:55:00 27 4
gpt4 key购买 nike

我的任务是编写一个 Perl 脚本(perl v 5.8 - 我公司使用的脚本),将文件从一个文件系统移动到另一个文件系统,并更新 Oracle DB 版本 11R2 中每个文件的位置。

我可以移动文件了。我更新数据库好了。当收到 Ctrl+C 信号时,我无法让脚本正常结束。原因是当脚本连接到数据库时按下 Ctrl+C 时。它会立即终止连接,并在执行查询之前返回错误代码。

我尝试按照几本书和在线出版物的建议来处理信号,但似乎并没有解决问题。通常需要几次尝试才能重现它,但最终我设法得到错误条件。

我在下面提供了我的代码的一些快照。

#!/usr/local/PERL/bin/perl -w
my $STOP_SIGNAL = 0;
$SIG{INT} = \&Shutdown_Handler;
$SIG{KILL} = \&Shutdown_Handler;
# I handle other signals too. No child processes on this script.

while ( $STOP_SIGNAL == 0 ) {
# Find some files
# Move some files
# Update DB with file new location and close db connection
# Establish a separate DB connecion to double check the transaction.
}

if ($STOP_SIGNAL) {
print "EXIT DUE TO Ctrl-C SIGNAL\n";
} else {
print "NORMAL EXIT\n";
}

sub Shutdown_Handler() {
$STOP_SIGNAL ++;
return;
}

有人遇到过这种情况吗?你是怎么解决的?谢谢丹。

环境:

PERL:
perl -version
This is perl, v5.8.1 built for i686-linux
DATABASE:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

最佳答案

嗯,在我的例子中,这对我来说是很好的工作。 (perl 5.18.2)

my $stop = 0;

$SIG{INT} = sub { ++$stop; };

while (!$stop) {
print "live\n";
sleep 1;
}

print "exit by signal" if ($stop);

关于linux - Perl 信号处理会截断与数据库的连接并导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25048021/

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