Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script optimization for waiting for a particular string in log files

Tags:

linux

bash

shell

I am using a bash script that calls multiple processes which have to start up in a particular order, and certain actions have to be completed (they then print out certain messages to the logs) before the next one can be started. The bash script has the following code which works really well for most cases:

tail -Fn +1 "$log_file" | while read line; do
    if echo "$line" | grep -qEi "$search_text"; then
        echo "[INFO] $process_name process started up successfully"
        pkill -9 -P $$ tail
        return 0
    elif echo "$line" | grep -qEi '^error\b'; then
        echo "[INFO] ERROR or Exception is thrown listed below. $process_name process startup aborted"
        echo "  ($line)  "
        echo "[INFO] Please check $process_name process log file=$log_file for problems"
        pkill -9 -P $$ tail
        return 1
    fi
done

However, when we set the processes to print logging in DEBUG mode, they print so much logging that this script cannot keep up, and it takes about 15 minutes after the process is complete for the bash script to catch up. Is there a way of optimizing this, like changing 'while read line' to 'while read 100 lines', or something like that?

like image 878
Matt Avatar asked May 14 '14 11:05

Matt


People also ask

How do you introduce a delay in shell script?

/bin/sleep is Linux or Unix command to delay for a specified amount of time. You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues. In other words, the sleep command pauses the execution on the next shell command for a given time.

Is there a wait command in bash?

The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.

How do I make bash script wait?

How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.

What is $@ bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


1 Answers

How about not forking up to two grep processes per log line?

tail -Fn +1 "$log_file" | grep -Ei "$search_text|^error\b" | while read line; do

So one long running grep process shall do preprocessing if you will.

Edit: As noted in the comments, it is safer to add --line-buffered to the grep invocation.

like image 127
Felix Frank Avatar answered Sep 30 '22 03:09

Felix Frank