Я хочу каким-то образом взять результат процесса и разделить его на партии. Например, в моем случае я хочу взять журналы из файла и отправить мне письмо каждый раз, когда есть запись.
Так что-то вроде этого:
tail -f /var/log/server.log | segment --delay=5 --command="sendmail me@example.com"
Вышеуказанная команда будет ждать, пока не будет 5 секунд бездействия, а затем вывести указанную команду с выходом до сих пор.
Есть ли что-нибудь подобное?
С помощью bash вы можете сделать что-то вроде:
segment () {
while true
do
read -t "$TMOUT" # read input with a timeout of $TMOUT seconds
printf -v output "%s\n" "$output" "$REPLY" # append to already read output
if (( $? == 0 ))
then
# timeout not exceeded, so we can continue reading
continue
fi
# some error occurred, run specified command with existing output
printf "%s\n" "$output" | "$@"
if (( $? > 128 ))
# timeout exceeded, so we discard mailed output and continue
output=""
else
# some other error, end loop
return
fi
done
}
Вы можете использовать его так:
tail -f /var/log/server.log | TMOUT=5 segment sendmail me@example.com