0

I am supposed to put the following command in the system function argument in a C program.

$ timed-run 20 prog1 1 1>/dev/null 2>abc.dat

timed-run is supposed to execute prog1 up to 20 seconds and then terminate it. I want the output of prog1 to be redirected to abc.dat. Is there any solution for this?

Thanks

4
  • 1 is stdout, 2 is stderr: timed-run 20 prog1 2>/dev/null 1>abc.dat . I didn't get your question - what isn't working? Do you need to write the program 'timed-run'? Commented Mar 24, 2011 at 19:52
  • Well, the timed-run script is already provided by expect package in linux. I want to discard the output of stdout but want to have the output of stderr i.e. 2 to be redirected to abc.dat. In fact there is also an argument to prog1 i.e. $ timed-run 20 prog1 1 2>abc.dat 1>/dev/null Commented Mar 24, 2011 at 20:08
  • So what about this isn't working when you tried it? Commented Mar 24, 2011 at 20:13
  • No. its not working. abc.dat is empty after execution. Commented Mar 24, 2011 at 20:22

3 Answers 3

1

When you write "1> /dev/null", you are redirecting the output of the program to the bit-bucket. (That is, you are discarding it.) If you write "1>filename" instead, the output will go to the named file.

Sign up to request clarification or add additional context in comments.

2 Comments

The desired output is infact being produced through stderr instead of stdout. So, I have to use 2>abc.dat instead of 1>abc.dat
So what exactly is your question? Note that the way you've written it, abc.dat will contain whatever timed-run writes to stderr. Possibly the timed-run program is not correctly dealing with the IO streams of prog1.
1

For redirecting child process output there are several ways, Following 2 are the most importants:

  1. popen() (The easy one to understand and use, but popen works only with stdout of child, but you can redirect stderr to stdout as well by appending to command line string "2>&1")
  2. pipe fork and exec trio will help you (there are lot of info about this functions in internet)

Comments

1

I suspect, but can't know without seeing the prog1 source code, that prog1 is not flushing its output buffers. That is, it has code that looks like:

i = 42;
fprintf(stderr, "result: %d\n", i);

The formatted output (i.e. "result: 42\n") is being stored in stderr's output buffer waiting for an opportune to be written. But, before that can happen, timed-run kills prog1 with a signal.

Your choices are to either call fflush(stderr) periodically, change the buffering type (with setbuf or setvbuf), or catch the signal and call fflush(stderr) at program termination.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.