Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with NFS latency in shell scripts

Tags:

bash

ksh

latency

I'm writing shell scripts where quite regularly some stuff is written to a file, after which an application is executed that reads that file. I find that through our company the network latency differs vastly, so a simple sleep 2 for example will not be robust enough.

I tried to write a (configurable) timeout loop like this:

waitLoop()
{
   local timeout=$1
   local test="$2"

   if ! $test
   then
      local counter=0
      while ! $test && [ $counter -lt $timeout ]
      do
         sleep 1
         ((counter++))
      done

      if ! $test
      then
         exit 1
      fi
   fi
}

This works for test="[ -e $somefilename ]". However, testing existence is not enough, I sometimes need to test whether a certain string was written to the file. I tried test="grep -sq \"^sometext$\" $somefilename", but this did not work. Can someone tell me why?

Are there other, less verbose options to perform such a test?

like image 432
andreas buykx Avatar asked Jul 03 '09 06:07

andreas buykx


People also ask

How do I make my shell script run faster?

There are few ways to make your shell (eg Bash) execute faster. Try to use less of external commands if Bash's internals can do the task for you. Eg, excessive use of sed , grep , awk et for string/text manipulation. If you are manipulating relatively BIG files, don't use bash's while read loop.

How is NFS latency measured?

Using avg RTT (Round Trip Time) field to determine NFS latency. The average Round Trip Time (avg RTT) in milliseconds is a good measurement for NFS latency. In below example, The avg RTT (Round Trip Time) column is the average latency of the connection in ms.

Is bash script slow?

Shell loops are slow and bash's are the slowest. Shells aren't meant to do heavy work in loops. Shells are meant to launch a few external, optimized processes on batches of data.


1 Answers

You can set your test variable this way:

test=$(grep -sq "^sometext$" $somefilename)

The reason your grep isn't working is that quotes are really hard to pass in arguments. You'll need to use eval:

if ! eval $test
like image 73
Dennis Williamson Avatar answered Oct 28 '22 13:10

Dennis Williamson