You've redirected the program's standard error stream to standard output, but this doesn't redirect the "Segmentation fault" message, because that message is not emitted by your program. Instead, it is emitted by the shell that called the program.
What you should do here depends on what your actual goal is.
If you really just want to redirect standard error to standard output, you can do that with your C program the same as with any command. Redirections like 2>&1
, as well as the other methods you used, work just fine. Your program does not actually write anything to standard error, but if you write a C program that does do so, then you'll see that it is redirected. You can use the fputs
or fprintf
function to write to stderr
. For example:
fprintf(stderr, "error: refusing to say hello world\n");
On the other hand, if your goal is to redirect the "Segmentation fault" messages your shell writes to standard error after a subprocess is terminated with SIGSEGV
, then you can write a compound command that contains the call to your program, and redirect from that compound command. You don't have to have any other command; as Gilles explains, it is sufficient to enclose your one command in {
;}
. For example, you can run:
{ ./hello.o 1000; } 2>>outfile
That appends all standard error output from running your program--both that which the program produces (which in this case is none) and that which the shell produces--to the file outfile
.
(I suspect, however, that you really do want to just redirect error output that your programs may actually produce, which is why I'm answering this rather than flagging it for closure as a duplicate.)
Although having your C program write to an obviously bogus address seems like a reliable way to trigger a segmentation fault deliberately, it really isn't. This is because C compilers are allowed to assume that undefined behavior doesn't occur, and some compilers will take advantage of this assumption even when not compiling with a high level of optimizations. To test the behavior of a program under a segmentation fault, I recommend you send it the SIGSEGV
signal while it is running. It can use the raise
function to do this to itself, or you can use the kill
or killall
command to do it.
For example, to test the example above, I just ran { sleep 1000; } >&out
and, in another terminal, killall -SEGV sleep
. (Since the sleep
command might be in use in the background, I should caution you that you might not want to follow that precise procedure on a production machine, at least not as a user who is doing other important stuff and certainly not as root.)
Finally, you might not want to name your executables with .o
suffixes, because these are typically used for object files produced by the compiler that must be linked together to produce a file that can actually be executed. On a Unix-like operating system, executable binaries are typically named with no extension.
{ ./hello.o <1.in } &>log
or similar to capture the standard error of the shell in addition to that of your program.) – thrig Nov 02 '17 at 05:23.o
extensions for complete executable programs is very non-standard. – G-Man Says 'Reinstate Monica' Nov 02 '17 at 05:37