Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7419643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:06:24+00:00 2026-05-29T08:06:24+00:00

I am trying to capture the output from tail in follow mode, where it

  • 0

I am trying to capture the output from tail in follow mode, where it outputs the text as it detects changes in the file length – particularly useful for following log files as lines are added. For some reason, my call to StandardOutput.Read() is blocking until tail.exe exits completely.

Relevant code sample:

var p = new Process() {
  StartInfo = new ProcessStartInfo("tail.exe") {
    UseShellExecute = false,
    RedirectStandardOutput = true,
    Arguments = "-f c:\\test.log"
  }
};
p.Start();

// the following thread blocks until the process exits
Task.Factory.StartNew(() => p.StandardOutput.Read());
// main thread wait until child process exits
p.WaitForExit();

I have also tried using the support for the OutputDataReceived event handler which exhibits the same blocking behavior:

p.OutputDataReceived += (proc, data) => {
  if (data != null && data.Data != null) {
    Console.WriteLine(data.Data);
  }
};
p.BeginOutputReadLine();

I do have a little bit more code around the call to StandardOutput.Read(), but this simplifies the example and still exhibits the undesirable blocking behavior. Is there something else I can do to allow my code to react to the availability of data in the StandardOutput stream prior to the child application exiting?

Is this just perhaps a quirk of how tail.exe runs? I am using version 2.0 compiled as part of the UnxUtils package.

Update: this does appear to be at least partially related to quirks in tail.exe. I grabbed the binary from the GnuWin32 project as part of the CoreUtils package and the version bumped up to 5.3.0. If I use the -f option to follow without retries, I get the dreaded “bad file descriptor” issue on STDERR (easy to ignore) and the process terminates immediately. If I use the -F option to include retries it seems to work properly after the bad file descriptor message has come by and it attempts to open the file a second time.

Is there perhaps a more recent win32 build from the coreutils git repository I could try?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:06:25+00:00Added an answer on May 29, 2026 at 8:06 am

    I know it is not exatly what you are asking but as James says in the comments, you could do the equivalent functionality directly in c# to save you having to launch another process.

    One way you can do it is like this:

    using System;
    using System.IO;
    using System.Text;
    using System.Threading;
    
    public class FollowingTail : IDisposable
    {
        private readonly Stream _fileStream;
        private readonly Timer _timer;
    
        public FollowingTail(FileInfo file,
                             Encoding encoding,
                             Action<string> fileChanged)
        {
    
            _fileStream = new FileStream(file.FullName,
                                         FileMode.Open,
                                         FileAccess.Read,
                                         FileShare.ReadWrite);
    
            _timer = new Timer(o => CheckForUpdate(encoding, fileChanged),
                               null,
                               0,
                               500);
        }
    
        private void CheckForUpdate(Encoding encoding,
                                    Action<string> fileChanged)
        {
            // Read the tail of the file off
            var tail = new StringBuilder();
            int read;
            var b = new byte[1024];
            while ((read = _fileStream.Read(b, 0, b.Length)) > 0)
            {
                tail.Append(encoding.GetString(b, 0, read));
            }
    
            // If we have anything notify the fileChanged callback
            // If we do not, make sure we are at the end
            if (tail.Length > 0)
            {
                fileChanged(tail.ToString());
            }
            else
            {
                _fileStream.Seek(0, SeekOrigin.End);
            }
        }
    
        // Not the best implementation if IDisposable but you get the idea
        // See http://msdn.microsoft.com/en-us/library/ms244737(v=vs.80).aspx
        // for how to do it properly
        public void Dispose()
        {
            _timer.Dispose();
            _fileStream.Dispose();
        }
    }
    

    Then to call for example:

    new FollowingTail(new FileInfo(@"C:\test.log"),
                      Encoding.ASCII,
                      s =>
                      {
                          // Do something with the new stuff here, e.g. print it
                          Console.Write(s);
                      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run curl as a process and capture the output from the
I'm trying to capture dvdauthor's output to a file. So far i found this
I have no idea why this is hanging. I'm trying to capture output from
I have been trying to capture stdout and stderr output from a DLL compiled
I am trying to capture stills from my MTS file from my Sony HD
I'm trying to call a C program from Java and capture the standard output.
I'm trying to capture output from a console application by running it in a
I'm trying to capture standard output of a command to a file-like object in
I am trying to capture the output of a tcpdump/grep pipeline from Python. I
I am trying to capture output from an install script (that uses scp) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.