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 6626331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:52:48+00:00 2026-05-25T21:52:48+00:00

I like to log a programs output ‘on demand’. Eg. the output is logged

  • 0

I like to log a programs output ‘on demand’. Eg. the output is logged to the terminal, but another process can hook on the current output at any time.

The classic way would be:

myprogram 2>&1 | tee /tmp/mylog

and on demand

tail /tmp/mylog

However, this would create a ever growing log file even if not used until the drive runs out of space. So my attempt was:

mkfifo /tmp/mylog
myprogram 2>&1 | tee /tmp/mylog

and on demand

cat /tmp/mylog

Now I can read /tmp/mylog at any time. However, any output blocks the program until the /tmp/mylog is read. I like the fifo to flush any incoming data not read back. How to do that?

  • 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-25T21:52:48+00:00Added an answer on May 25, 2026 at 9:52 pm

    Inspired by your question I’ve written a simple program that will let you do this:

    $ myprogram 2>&1 | ftee /tmp/mylog

    It behaves similarly to tee but clones the stdin to stdout and to a named pipe (a requirement for now) without blocking. This means that if you want to log this way it may happen that you’re gonna lose your log data, but I guess it’s acceptable in your scenario.
    The trick is to block SIGPIPE signal and to ignore error on writing to a broken fifo. This sample may be optimized in various ways of course, but so far, it does the job I guess.

    /* ftee - clone stdin to stdout and to a named pipe 
    (c) racic@stackoverflow
    WTFPL Licence */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <signal.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
        int readfd, writefd;
        struct stat status;
        char *fifonam;
        char buffer[BUFSIZ];
        ssize_t bytes;
        
        signal(SIGPIPE, SIG_IGN);
    
        if(2!=argc)
        {
            printf("Usage:\n someprog 2>&1 | %s FIFO\n FIFO - path to a"
                " named pipe, required argument\n", argv[0]);
            exit(EXIT_FAILURE);
        }
        fifonam = argv[1];
    
        readfd = open(fifonam, O_RDONLY | O_NONBLOCK);
        if(-1==readfd)
        {
            perror("ftee: readfd: open()");
            exit(EXIT_FAILURE);
        }
    
        if(-1==fstat(readfd, &status))
        {
            perror("ftee: fstat");
            close(readfd);
            exit(EXIT_FAILURE);
        }
    
        if(!S_ISFIFO(status.st_mode))
        {
            printf("ftee: %s in not a fifo!\n", fifonam);
            close(readfd);
            exit(EXIT_FAILURE);
        }
    
        writefd = open(fifonam, O_WRONLY | O_NONBLOCK);
        if(-1==writefd)
        {
            perror("ftee: writefd: open()");
            close(readfd);
            exit(EXIT_FAILURE);
        }
    
        close(readfd);
    
        while(1)
        {
            bytes = read(STDIN_FILENO, buffer, sizeof(buffer));
            if (bytes < 0 && errno == EINTR)
                continue;
            if (bytes <= 0)
                break;
    
            bytes = write(STDOUT_FILENO, buffer, bytes);
            if(-1==bytes)
                perror("ftee: writing to stdout");
            bytes = write(writefd, buffer, bytes);
            if(-1==bytes);//Ignoring the errors
        }
        close(writefd); 
        return(0);
    }
    

    You can compile it with this standard command:

    $ gcc ftee.c -o ftee

    You can quickly verify it by running e.g.:

    $ ping www.google.com | ftee /tmp/mylog

    $ cat /tmp/mylog

    Also note – this is no multiplexer. You can only have one process doing $ cat /tmp/mylog at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to capture output from a UNIX process but limit max file
I'd like to log the output of a command to stdout as well as
I'd like to write log to 2 different log files from the same process.
Basically what I'd like to do is have two output terminal windows for a
Some commands like svn log, for example will only take one input from the
I'd like to log the number of active threads created by a newCachedThreadPool call.
We have multiple log files like database log, weblog, quartzlog in our application. Any
Using log4net we would like to log all calls to our ASP.NET MVC controller
I'm writing an application that needs a log-like view, (similar to how an IM
I have an odd exception in our application and I'd like to log when

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.