Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, from a bash script, i invoke a program ./foobar that creates a logfile.txt . Now I want to view the contents of logfile.txt while ./foobar is writing to it, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    2

    tail -f until another program exists?


    Hi,

    from a bash script, i invoke a program ./foobar that creates a logfile.txt . Now I want to view the contents of logfile.txt while ./foobar is writing to it, but only until ./foobar finished.

    So, e.g. I could use:
    Code:
    ./foobar &
    tail -f logfile.txt
    ...but then I get stuck in tail.

    Is there an (elegant) way to do this?

    Lars

  2. #2
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    ./foobar | less

    Surely not exactly what you're looking for, but this is the best thing I come up with.

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    To slightly modify (i.e., complicate) K's suggestion:
    Code:
    #!/bin/sh
    ./foobar >logfile.txt 2>&1 &
    while ps -p $! >/dev/null 2>&1; do
      clear
      cat logfile.txt
      sleep 1
    done

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,404
    Change foobar to write to stdout. Then you can do this:
    Code:
    ./foobar | tail -f
    If you still want the output to go to logfile.txt, then you can insert the tee command, as in:
    Code:
    ./foobar | tee logfile.txt | tail -f
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Oct 2011
    Posts
    2

    solved

    thanks everybody.
    today, i found another solution: tail has an --pid option!!
    Code:
    ./foobar &
    foobar_pid=$!
    tail -f logile.txt --pid=$foobar_pid

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    That is completely the best way, thanks for posting solution!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •