tail -f、ログが3秒間アイドル状態の後に改行文字を挿入しますか?

tail -f、ログが3秒間アイドル状態の後に改行文字を挿入しますか?

実行中にtail -f error.log3秒間ファイルに何も追加されなかった後にプログラムで改行文字を挿入するにはどうすればよいですか?

(明らかに1つの改行を追加した場合は、追加のテキスト行がログファイルに追加されるまで別の改行を追加しないでください。)

たとえば、次の行は error.log に追加されます。

foo
bar
boo [[wait 4 seconds]]
2far
2foo
2bar
2boo [[wait 40 seconds]]
2far

コンソールの出力は次のとおりです。

foo
bar
boo

2far
2foo
2bar
2boo

2far

ベストアンサー1

いつでも手動で実行できますtail -f(ここでコメントを削除しない限り、ファイル全体をダンプするseek()のと似ています)。tail -n +1 -fperl

perl -e '
  $| = 1;
  # seek STDIN, 0, 2; # uncomment if you want to skip the text that is
                      # already there. Or if using the ksh93 shell, add
                      # a <((EOF)) after < your-file
  while (1) {
    if ($_ = <STDIN>) {
      print; $t = 0
    } else {
      print "\n"            if $t == 3;
      # and a line of "-"s after 10 seconds:
      print "-" x 72 . "\n" if $t == 10;
      sleep 1;
      $t++;
    }
  }' < your-file

または、3秒間入力がない場合は、tail -ftail操作を実行してそれを使用して改行を挿入します。perl

tail -f file | perl -pe 'BEGIN{$SIG{ALRM} = sub {print "\n"}} alarm 3'

これは、出力自体が遅くならないと仮定します(出力が積極的に読み取られないパイプに入るときなど)。

おすすめ記事