Tail file till process exits
<p>Going through the answers at <a href="http://superuser.com/questions/270529/monitoring-a-file-until-a-string-is-found">superuser</a>.</p>

<p>I'm trying to modify this to listen for multiple strings and echo custom messages such as ; 'Your server started successfully' etc</p>

<p>I'm also trying to tack it to another command i.e. pip</p>

<pre><code>wait_str() {
  local file="$1"; shift
  local search_term="Successfully installed"; shift
  local search_term2='Exception'
  local wait_time="${1:-5m}"; shift # 5 minutes as default timeout

  (timeout $wait_time tail -F -n0 "$file" &amp;) | grep -q "$search_term" &amp;&amp; echo 'Custom success message' &amp;&amp; return 0 || || grep -q "$search_term2" &amp;&amp; echo 'Custom success message' &amp;&amp; return 0

  echo "Timeout of $wait_time reached. Unable to find '$search_term' or '$search_term2' in '$file'"
  return 1
}
</code></pre>

<p>The usage I have in mind is:</p>

<pre><code>pip install -r requirements.txt &gt; /var/log/pip/dump.log &amp;&amp; wait_str /var/log/pip/dump.log
</code></pre>

<p>To clarify, I'd like to get wait_str to stop tailing when pip exits, whether successfully or not.</p>

<p>Following is general answer and <code>tail</code> could be replaced by any command that result in stream of lines. </p>

<p>IF different string needs <strong>different</strong> actions then use following: </p>

<pre><code>tail -f var/log/pip/dump.log |awk '/condition1/ {action for condition-1} /condition-2/ {action for condition-2} .....' 
</code></pre>

<p>If multiple conditions need <strong>same</strong> action them ,separate them using OR operator :</p>

<pre><code>tail -f var/log/pip/dump.log |awk '/condition-1/ || /condition-2/ || /condition-n/ {take this action}'
</code></pre>

<p>Based on comments : Single <code>awk</code> can do this. </p>

<pre><code>tail -f /path/to/file |awk '/Exception/{ print "Worked"} /compiler/{ print "worked"}'  
</code></pre>

<p>or </p>

<pre><code>tail -f /path/to/file | awk '/Exception/||/compiler/{ print "worked"}' 
</code></pre>

<p>OR Exit if match is found   </p>

<pre><code>tail -f logfile |awk '/Exception/||/compiler/{ print "worked";exit}'
</code></pre>

python linux bash shell pip