tail -f, вставить разрыв строки после того, как журнал простаивает в течение 3 секунд?

Вам не нужен parallel , поскольку тело цикла не зависит от предыдущего итераций. Просто запустите новый фоновый процесс для каждого хоста. Однако

while read host; do
    sortstuff.sh -a "$host" > sortedstuff-"$host" &
done < live_hosts
wait    # Optional, to block until the background tasks are done

parallel действительно упрощает управление некоторыми аспектами; вы можете более легко ограничить количество параллельно выполняемых заданий.

14
22.02.2018, 16:09
3 ответа

Siempre puede implementar eltail -f(bien aquí, a menos que elimine el comentario del seek(), más como tail -n +1 -fya que estamos volcando todo el archivo )a mano con perlpor ejemplo:

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

O deje que tail -fhaga el seguimiento y use perlpara insertar las nuevas líneas si no hay entrada durante 3 segundos:

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

Asumen que la salida en sí no se ralentiza (como cuando la salida va a una tubería que no se lee activamente ).

14
27.01.2020, 19:50

bash +datesolución:

while IFS= read -r line; do        
    prev=$t         # get previous timestamp value
    t=$(date +%s)   # get current timestamp value
    [[ ! -z "$prev" ]] && [[ "$((t-prev))" -ge 3 ]] && echo ""
    echo "$line"    # print current line
done < <(tail -f error.log)
8
27.01.2020, 19:50

Pythonsolución (con argumento dinámico intervalo de tiempo ):

tailing_by_time.pyguión:

import time, sys

t_gap = int(sys.argv[1])    # time gap argument
ts = 0
while True:
    line = sys.stdin.readline().strip()    # get/read current line from stdin
    curr_ts = time.time()                  # get current timestamp
    if ts and curr_ts - ts >= t_gap:
        print("")                          # print empty line/newline
    ts = curr_ts
    if line:
        print(line)                        # print current line if it's not empty

Uso:

tail -f error.log | python tailing_by_time.py 3
6
27.01.2020, 19:50

Теги

Похожие вопросы