Linux – Wait until a specific pattern is written on log using tail and sed

In the Ant build.xml, i need to make sure the JBoss server is started before running the test cases. There are 2 possible approaches.

  • Using twiddle.sh to check the status
  • Monitoring server.log until “Started in” is written on it

 

I took the 2nd approach. Here is an example output log when the JBoss server is started successfully.

  • 2013-10-07 10:06:33,657 INFO [org.jboss.bootstrap.microcontainer.ServerImpl] (main) JBoss (Microcontainer) [5.1.0 (build: SVNTag=JBPAPP_5_1_0 date=201009150028)] Started in 39s:984ms
  •  

    In the build.xml, the sshexec task is used and keep checking the remote server log.

    <sshexec
      trust="true"
      host="<host>"
      username="<ssh user>" keyfile="<key file>"
      command="<command to be run on the remote server>" />
    

     

    I got this working at first

    <!-- command: tail -n 0 -f <server.log> | sed --quiet '/Started in/ q' -->
    <sshexec
      trust="true"
      host="<host>"
      username="<ssh user>" keyfile="<key file>"
      command="tail -n 0 -f <server.log> | sed --quiet '/Started in/ q'" />
    

     

    But it only quits the tail command until a new line is written to the log.

    Then i come up with this solution. Thanks StackOverflow!

    <!-- command: sed --quiet '/Started in/ q' <(tail -n 0 -f <server.log>) -->
    <sshexec
      trust="true"
      host="<host>"
      username="<ssh user>" keyfile="<key file>"
      command="sed --quiet '/Started in/ q' &lt;(tail -n 0 -f <server.log>)" />
    

     

    Unfortunately, it doesn’t work in my case. I have to wait for a few JBoss server to complete the startup in Ant script but it turns out the above approach only works for the first JBoss server. For the others, it only exits the tail command when a new line is written to the server.log.

    Finally i got another solution which will write the log file once the string is found.

    <!-- command: sed --quiet '/Started in/ q' <(tail -n 0 -f <server.log>) -->
    <sshexec
      trust="true"
      host="<host>"
      username="<ssh user>" keyfile="<key file>"
      command="tail -f <server.log> | grep -m 1 'Started in' | xargs echo '' >> <server.log>" />
    

     

    There are other suggested solutions. You can refer to the references below.

    Done =)

    Reference:

    Advertisements

One thought on “Linux – Wait until a specific pattern is written on log using tail and sed”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s