When I press Ctrl + C in a linux console running a Mathematica script, it doesn't stop. Instead it brings a console where I am supposed to type either quit, or exit, or a couple other options. I think this is at odds with linux consistency and it is pretty annoying.

For example, when I write a .sh script containing various calls to a Mathematica script, pressing Ctrl + C won't stop the script, because Mathematica captures the SIGINT signal, brings the annoying Interrupt console, and whatever after I type here, the script just keeps running because it never heard the SIGINT signal (at least that's what I think is happening).

The only way I have to kill the script is to close the terminal that spawned it, which is pretty brutal.

My question is: Why Mathematica doesn't respect Ctrl + C signals, like most programs in Linux do? What's the design? I love Mathematica, but this particular point is not very friendly.

And on a practical side: How do I stop a shell script which calls multiple Mathematica scripts, without closing the terminal?

  • $\begingroup$ Maybe you can try to kill only the process with the command killor killall. You can get the process name and number with the command ps -acx. $\endgroup$
    – SquareOne
    Jan 21, 2015 at 15:18

2 Answers 2


That's common in more than one case, for example, python also intercepts signals, but usually it exits, unless it is in interactive mode, where Ctrl C will stop the current computation but not exit the interactive shell. Going back to your problem, if your mathematica script won't need any input, you can redirect /dev/null as input, and then your calling script will keep the keyboard binding.

This is a simple M script (let's call it testsignal.m):

#!/usr/local/bin/MathematicaScript -script 
Do[Print[i]; Pause[1], {i, 10}];

Then, you can run it, and try Ctrl C:

[tmp]$ ./testsignal.m 
    Interrupt> quit

It shows the intercepted message from the kernel, and you need to exit yourself, but trying it with the input redirect:

[tmp]$ ./testsignal.m < /dev/null 

So, using the redirected might do the trick. By the way, that problem does not happen when you use math -noprompt < inputfile.m, on those cases Ctrl C does the expected behavior, so, that's another way to avoid the problem too.


We could do with some more information for a fuller answer. For instance why do you want to stop it? Why are you running multiple MMA scripts? Why not tie them together in package(s) with a controlling notebook?

If I had to guess why ctrl-c isn't working its probably because somewhere on the way there are sub-processes being spawned that aren't don't have the parent pid of your window. Probably has a parent pid of the Kernel or similar.

On the assumption its because one of the scripts that have been called is throwing an error or otherwise returning an incorrect result you could try the below.

You need to see if you can get MMA to produce correct return codes then you can test them and abort the script - see here for more information and code snippet below.

ls -al file.ext
rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi

A messier workaround is to get each MMA script to check a global variable at the start of each script or package, then set it manually to some value that indicates halt from a separate notebook window.

  • $\begingroup$ I am using a script because I am running Mathematica stuff as well as calling other programs, all together. I want to stop it mostly for debugging processes (the usual start, stop, repair, restart routine). $\endgroup$
    – a06e
    Jan 14, 2015 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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