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

  • Home
  • SEARCH
  • 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 8038185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:09:02+00:00 2026-06-05T03:09:02+00:00

I would like to catch all the inputs and output of a commandline program

  • 0

I would like to catch all the inputs and output of a commandline program (namely, GDB, but currently changed to ls or cat for simplicity) and redirect it into a file, for later analysis.

I couldn’t get anything close to working, but I can’t understand what’s wrong.
Here is my last attempt:

#!/usr/bin/env python2

import subprocess
import sys
import select
import os

def get_pipe():
  fd_r, fd_w = os.pipe()
  return os.fdopen(fd_r, "r"), os.fdopen(fd_w, "w")

out_r, out_w = get_pipe()
err_r, err_w = get_pipe()
in_r, in_w = get_pipe()

proc = subprocess.Popen(["ls"] + sys.argv[1:], stdin=in_r, stdout=out_w, stderr=err_w)

out_w.close()
err_w.close()
in_r.close()

running = True
while running:
  input_lst, output_lst, x_lst = select.select([sys.stdin],[out_r, err_r],  [], 0.5)

  if out_r in output_lst+input_lst:
    data = out_r.readlines()
    print "*",data,"*"
  if err_r in output_lst+input_lst:
    data = err_r.readlines()
    print "+",data,"+"
  if sys.stdin in input_lst:
    data = sys.stdin.readline()
    print "-", data, "-"
    in_w.write(data)

  # don't try to quit if we received some data
  if len(input_lst+output_lst+x_lst) != 0:
    continue
  retcode = proc.poll()
  if retcode is not None:
    running = False

out_r.close()
err_r.close()
in_w.close
exit(retcode)

I tried several other options, like
– writing a file wrapper, which was supposed to write to an external file everything written into stdin / read from stdout-err
– named pipes
– …

but the best I obtained was the very first lines of “ls”.

Moreover, GDB relies on readline for CLI edition, and I feel like it won’t be that easy to catch transparently!

  • 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-06-05T03:09:04+00:00Added an answer on June 5, 2026 at 3:09 am

    So after quite a while of research, I found a solution to this problem:

    with non blocking reads and writes, we just have to wait for the input file to run out of data (and thrown an exception), and then operate on the same for the output (stdout and stderr):

    #!/usr/bin/python2
    
    import sys, os
    import subprocess
    import fcntl
    
    dump = open("/tmp/dump", "w")
    dump.write("### starting %s ###" % " ".join(sys.argv))
    
    proc = subprocess.Popen(["<real app>"] + sys.argv[1:], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    def nonblocking(fd):
      fl = fcntl.fcntl(fd, fcntl.F_GETFL)
      fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    
    nonblocking(proc.stdin)
    nonblocking(proc.stdout)
    nonblocking(proc.stderr)
    
    nonblocking(sys.__stdin__)
    nonblocking(sys.__stdout__)
    nonblocking(sys.__stderr__)
    
    def me_to_proc():
      x_to_y(sys.__stdin__, proc.stdin, "~in> ")
    
    def proc_to_me():
      x_to_y(proc.stdout, sys.__stdout__, "<out~ ")
    
    def proc_to_me_err():
      x_to_y(proc.stderr, sys.__stderr__, "<err~ ")
    
    def x_to_y(x, y, prefix=""):
      try:
        while True:
           line = x.readline()
           to_dump = "%s%s" % (prefix, line)
           print >> dump, to_dump
           print to_dump
           y.write(line)
           y.flush()
           dump.flush()
      except:
        pass
    
    recode = None
    while recode is None:
      proc_to_me()
      #proc_to_me_err()
      me_to_proc()
    
      retcode = proc.poll()
    
    exit(retcode)
    

    just replace your original binary with this script, and change <real app> to create the actual process.
    In- and out-put information will be written on screen and dumpted into /tmp/dump.

    (I’m not sure however about the termination criteria, I didn’t check that in details)

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

Sidebar

Related Questions

I would like to catch all dev tags and their respective content, through php
I have a base class, and I would like to catch all exceptions of
I am currently playing around with chrome extensions. I would like to catch google
I would like a catch-all display of any errors or exceptions. For instance I
I would like to catch all unhandled exceptions in my UI app in C#,
I would like to catch all exceptions in a Rails 3 application. I tried
I would like to test some exception handling logic in the empty catch block
I have a Try Catch in my code. I would like to know if
Would like to get a list of all packages available on CRAN to the
In a GNU makefile, I would like to set an output variable to one

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.