If a user hits Control-C when a script is running, they get an Interrupt> prompt, where they have to type exit or quit to return to the shell. I'd like to have the interpreter exit immediately when Control-C is hit. To this end, I'd like to catch the interrupt and run Exit. Is there a way to do this? Or is there a better option?
Answer
Here's an ugly hack that works, based directly on this answer. This doesn't work on Windows.
First create a LibraryLink function that can set an alternate signal handler:
<< CCompilerDriver`
handlerlib = "
#include
#include
#include
#include \"WolframLibrary.h\"
// you may want to specialize this for SIGINT only
void my_handler(int s){
printf(\"Caught signal %d\\n\",s);
exit(1);
}
DLLEXPORT mint WolframLibrary_getVersion(){
return WolframLibraryVersion;
}
DLLEXPORT int WolframLibrary_initialize( WolframLibraryData libData) {
return 0;
}
DLLEXPORT void WolframLibrary_uninitialize( WolframLibraryData libData) {
return;
}
DLLEXPORT void setHandler(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = my_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
return LIBRARY_NO_ERROR;
}
";
CreateLibrary[handlerlib, "handlerlib"]
Once the library is compiled, you can use it in any Mathematica session on the same machine.
So just put this at the beginning of your script:
LibraryFunctionLoad["handlerlib", "setHandler", {}, "Void"][]
Now Ctrl-C will kill the script immediately.
You could suggest to support that this be the default behaviour when Mathematica is invoked using the -script option.
Comments
Post a Comment