| 1 | #!/bin/bash |
| 2 | # Desc: Writes stdin to disk every 5 minutes |
| 3 | |
| 4 | yell() { echo "[$(date --iso-8601=ns)]:$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique |
| 5 | die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370 |
| 6 | try() { "$@" || die "cannot $*"; } #o |
| 7 | |
| 8 | declare -a buffer # Initialize buffer array |
| 9 | scriptTTL="15"; |
| 10 | bufferTTL="5"; |
| 11 | |
| 12 | magicWriteBuffer() { |
| 13 | printf "%s\n" "${buffer[@]}" | cat | cat | cat >> /dev/shm/$(date +%s)..bkFreqWrite-output1.txt; |
| 14 | printf "%s\n" "${buffer[@]}" | cat | cat | cat >> /dev/shm/$(date +%s)..bkFreqWrite-output2.txt; |
| 15 | printf "%s\n" "${buffer[@]}" | cat | cat | cat >> /dev/shm/$(date +%s)..bkFreqWrite-output3.txt; |
| 16 | } |
| 17 | |
| 18 | bufferRound=0; |
| 19 | # Run until script TTL seconds pass |
| 20 | while [[ $SECONDS -lt "scriptTTL" ]]; do |
| 21 | bufferTOD="$((SECONDS + $bufferTTL))"; |
| 22 | lineCount=0; |
| 23 | # Consume stdin to fill buffer until buffer time-of-death (TOD) arrives |
| 24 | while read -r line && [[ $SECONDS -lt "$bufferTOD" ]]; do |
| 25 | # Append line to buffer |
| 26 | buffer+=("$line"); |
| 27 | echo "Processing line:$lineCount"; |
| 28 | echo "Current line :$line"; |
| 29 | echo "buf elem count :${#buffer[@]}"; |
| 30 | ((lineCount++)); |
| 31 | done; |
| 32 | # Export buffer to asynchronous processing. |
| 33 | magicWriteBuffer & |
| 34 | unset buffer; |
| 35 | # Increment buffer round |
| 36 | ((bufferRound++)); |
| 37 | done; |
| 38 | |
| 39 | # Author: Steven Baltakatei Sandoval |
| 40 | # License: GPLv3+ |