I can redirect stdout and stderr of a program using ./a.out > output.txt 2> error.txt

But these don't redirect messages like segmentation faults.
So I found

{ ./a.out < $TEST_DIR'test'$i'.in' > $OUTPUT_DIR/output$i.txt ; } 2> $OUTPUT_DIR/error$i.txt

Yet, the core dump and stack smash messages are not getting redirected.
How do redirect them?


The "Segmentation Fault" messages are not printed by the faulting program, but by the shell.

The *** stack smashing detected *** & backtrace + memmap messages (at least on my system) are printed by the stack protector handler directly to the the controlling terminal (_PATH_TTY/dev/tty is opened directly, with no regard to stdout or stderr, then the messages are written there -- see fortify_fail.c and libc_fatal.c in glibc).

If you want to catch the whole thing, run your your program with script(1) (eg script -c './rpneval ...') or something similar.

| improve this answer | |
  • I tried script. But did not help. Here is the command I used: script -q -c '{ ./a.out < '$INPUT' > '$OUTPUT' ; } 2> '$OUTPUT – Jithin Pavithran Sep 14 '18 at 18:24
  • 1
    run it like this: script -c './a.out < IN > OUT' ERR >/dev/null, replacing IN, OUT, ERR with the files where/from you want those things redirected. – mosvy Sep 14 '18 at 18:36
  • What is ERR >/dev/null here? It is not making sense to me. – Jithin Pavithran Sep 14 '18 at 18:42
  • 1
    ERR is the name of the output file of script(1) (please read the manpage). You can omit it and have the output in the file named typescript. As to the >/dev/null you can omit it if you want the output both in the output file and in the terminal. And do not use the -q switch to script -- it prevents any output whatsoever. – mosvy Sep 14 '18 at 18:48
  • It worked like a charm. Nice. Thank you. I think you should update your answer with the content of this comment. I have seen many similar questions without proper answers. This is helpful. – Jithin Pavithran Sep 14 '18 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.