问题
One of the binaries which I am using in my shell script is causing a segmentation fault (RETURN VALUE: 139)
And even though, I am redirecting both stdout and stderr to a logfile, the Segmentation Fault error messages is displayed in the terminal when I am running the shell script.
Is it possible to redirect this message from Segfault to a logfile ??
回答1:
The Segmentation Fault message you see is printed by the shell that is running your program. This behavior varies from shell to shell, so a few things you could try (if you insist on getting the segmentation fault message into your logs from shell-redirects).
# Have sh invoke your program, and redirect output from both sh and your program into logfile
sh -c "program arguments more arguments" >logfile 2>&1
# Force bash to not just exec your program (/bin/true part), and redirect output
# from both bash and your program into logfile
bash -c "/bin/true; program arguments more arguments" >logfile 2>&1
回答2:
Well, I am answering my own question.. :) I found the answer here How can I suppress the output due to a SIGSEGV or a SIGFPE in a Fortran 90 program?
The trick is to add
`exec 2> filename`
in the shell script.
This will redirect all the messages from the shell to a log file
回答3:
try
./program &> logfile
there are various exampls on I/O redirection here, have a look
You can take a look at this discussion as well
来源:https://stackoverflow.com/questions/2325152/check-for-stdout-or-stderr