2015-02-05 67 views
0

我写了超级简单的脚本,它可以旋转我的日志文件并无缝地拖曳它们。无法停止脚本中的循环

我的问题,我不能按Ctrl +ç程序,我需要按Ctrl +ž后杀死它。

我该如何解决这个问题? 我试过perl,但我有一个味精“闹钟”,我想避免。 与perl我可以Ctrl + C它很好。

perl -e "alarm 10; exec @ARGV" "tail -15f $line | filter" 

而且我的代码:

#!/bin/bash 
    # 
    while : 
    do while read line 
      do 
        charcount=$(ls $line | awk '{ print length; }') 
        printf '%0.s=' $(seq 1 $charcount) 
        echo -e "\n$line" 
        printf '%0.s=' $(seq 1 $charcount) 
        printf '\n' 
        timeout 10s tail -15f $line | filter 
      done < <(ls /var/log/net/*.log) 
    done 

谢谢!

+0

你究竟想在这里完成什么?我怀疑你只是在两种不同的语言中建立超时和警报而给自己带来痛苦。我建议在纯perl中重新实现。 – Sobrique 2015-02-05 15:47:24

回答

1

您提到的'闹钟'警报是因为这是处理SIGALRM信号的默认方式,该信号由perl中的alarm函数生成。

为什么Ctrl-C不能正常工作的原因是因为发生了阻塞呼叫,所以发送的SIGINT信号不会被捕获和处理。我看不到任何明显的代码会导致这个问题。

在我看来,最明显的方法是 - 停止混合perl和bash,因为这种方式就是疯狂。

如何在Perl中使用File::Tail模块?这甚至对如何做你想要什么样的例子:

#!/usr/bin/perl 

use strict; 
use warnings; 

use File::Tail; 

foreach (@ARGV) { 
    push(@files, File::Tail->new(name => "$_", debug => $debug)); 
} 
while (1) { 
    ($nfound, $timeleft, @pending) = 
     File::Tail::select(undef, undef, undef, $timeout, @files); 
    unless ($nfound) { 

     # timeout - do something else here, if you need to 
    } 
    else { 
     foreach (@pending) { 
      print $_->{"input"} . " (" 
       . localtime(time) . ") " 
       . $_->read; 
     } 
    } 
} 

这可能会允许你ctrl-c同时运行反正它,但Perl并允许您通过%SIG哈希有超过信号更好的控制 - 允许您定义杀死信号的自定义处理程序 - 如从alarmSIGINTCtrl-C

+0

我对perl并不熟悉,谢谢你的信息,我会检查一下。 – Chura 2015-02-06 16:51:45