August 8th, 2013, 10:38 AM
Call external command check for core dumped
I have to debug someone's code and I don't know much about Perl. I tried to google but found no solution. I am puzzled that I found no similar problem online. I am stuck at trying to display an error message or copy a file when there is a core dumped calling external program. This is the code:
open CMD, "$cmd 2>&1 |"
or die `cp $StdInFile /tmp`;
open CMD, "$cmd 2>&1 |"
or die "cmd failed: $cmd";
The program exists and working. However, the program fails whenever user enters bad inputs and it core dump. When core dump it never executes die. Why? How can I get it to execute die when there is a core dump? Am I missing something here?
August 8th, 2013, 05:22 PM
Hmm, not sure I will be able to help very much. First, the syntax used in your commands is unknown to me and looks pretty strange to me.
Second, if you get a core dump, it means that your system crashed (well, almost crashed, it was able to recover, but only by killing the faulty process -- my explanation is probably not very accurate technically, but that's the idea.). In other words, it is not a Perl error, but a Unix error when running the command issued by the Perl script. Perl cannot control that.
The die function will enable you to report an error internal to your program (e.g. @_ contains only two items, and the subroutine exprects three), a system call error (the system call was not able to open the file or the socket) or even a system command (with the backticks or the system function) if it returns a failure status (it was impossible to create the directory called for in the `mkdir $foo`, perhaps because it already existed or because you had unsufficient privileges), but it cannot catch a system command running amok to the point that the OS had to kill it in emergency to avoid losing control, which is what is usually happening when Unix creates a code dump (perhaps the command is exhausting the full system memory).
August 8th, 2013, 07:54 PM
You should be able to trap the core dump if you use IPC::System::Simple to run your command instead of using open.
August 8th, 2013, 09:52 PM
Thank you for clearing that up. I thought that was the case.
Originally Posted by Laurent_R
August 8th, 2013, 09:54 PM
Thank you for your suggestion. I will try that.
Originally Posted by FishMonger