Based on this I'd like to close the mathematica front end without the nagging dialog boxes that follow.

Why am I trying to do this?

I have a bash script which is such:


mathematica bfile01.nb
pid1=$(pgrep mathematica)
kill -9 $pid1

! [ -z `pidof mathematica` ] || mathematica bfile02.nb
pid2=$(pgrep mathematica)
kill -9 $pid2

! [ -z `pidof mathematica` ] || mathematica bfile03.nb

So this is just a serial execution of several mathematica files, each of which has an initialization cell in it.

I find that unless I close the front end, the next file doesn't launch and killing the process in the unix terminal didn't quite help (very strange as this only happens with mathematica)

So any thoughts or ideas?

Edit 2: Using a mathematica script file

Here is a mathematica file that I converted into a script: When I chmod it and run it, I get a bunch of error messages which don't allow my figures to be saved from the Plot3D or ContourPlot.

If you try to run the file attached (test3.m), you would want to change the file save path, Export in the last line of the file.

Error messages:

> General::stop: Further output of ReplaceAll::reps will be suppressed
> during this calculation.

Edit 3:

In the test3.m file, I made the following changes:

Changed hSol = to hSol[x_?NumericQ, y_?NumericQ, t_?NumericQ] (file has been uploaded).

This has changed the error messages that I get to:

> Part::partw: Part 3 of InterpolatingFunctionDomain[hSol] does not
> exist.
> Set::shape: Lists {TMin, TRup} and
> InterpolatingFunctionDomain[hSol][[3]] are not the same shape.
> Set::shape: Lists {nX, nY, nT} and {} are not the same shape.

Clearly, when run as a mathematica notebook, I never got these errors.

Edit 4:

I seem to have corrected my errors after Ruebenko's last comment.

the new file is attached here.

Heres the issue that I had:

Once the package was created (.m file), when opened in a text editor, all the lines were commented out with (**) which I don't know why.

However, getting rid of the comments also uncommented some text that I had previously commented so as to not use. That resulted in error messages.

  • $\begingroup$ Perhaps this is helpful: ref/program/MathematicaScript $\endgroup$
    – user21
    Aug 15, 2012 at 22:38
  • $\begingroup$ @ruebenko It seems to lack proper documentation though. $\endgroup$
    – dearN
    Aug 15, 2012 at 22:53
  • 1
    $\begingroup$ seems fully documented to me. This is not a Mathematica function. Have a look in the more into and the tutorial tutorial/MathematicaScripts. $\endgroup$
    – user21
    Aug 15, 2012 at 22:55
  • $\begingroup$ @ruebenko Hmmm... this needs some consideration. Thank you. I didn't quite understand that what you included in your comment was the actual path to the tutorial. Will look into that. However, what if I am compelled to use bash scripting exclusively without turning my mathematica file into a script? (Just playing devils adv.) $\endgroup$
    – dearN
    Aug 15, 2012 at 22:58
  • 1
    $\begingroup$ Excellent. Have fun with the scripting. $\endgroup$
    – user21
    Aug 16, 2012 at 16:53

2 Answers 2


You may want to try MathematicaScrip which was made for exactly this purpose.

Documentation for that is ref/program/MathematicaScript and perhaps tutorial/MathematicaScripts

  • $\begingroup$ Done! :) Hopefully I won't run into too many issues. But obv. I'd still be interested in the front end tokens $\endgroup$
    – dearN
    Aug 16, 2012 at 17:08

Take a look at: Programmatically quitting the FrontEnd or running without one?

I asked some similar questions and the answers have some good strategies to do this although most of them as workarounds outside of the Mathematica front end.

A simple but key thing, adding


to the end of your initializations cell can avoid some of the problems with pop up dialog boxes.

You may find:



But I did not find a way to quit Mathematica with a FrontEndToken even if it directly followed saving the notebook, because unfortunately, executing a single command changes the notebook's state, which then prompts one to save the notebook on exit.

A nice to have -- running a command in a sandbox that wouldn't affect the Front End''s state.

No clue how to do it, but it could prove useful.

In my application I eventually ran a script on a timer and had to gauge when the notebook completed the processing it needed.

One final thought/question. Does your application require the front end to run? You can run the kernel even multiple kernels for parallel processing without a front end.

Some potentially useful links:

  • $\begingroup$ Will read all this and see what seems to do the trick. And then I'll include that here! Thanks! $\endgroup$
    – dearN
    Aug 16, 2012 at 0:24
  • $\begingroup$ You could use SetOptions[EvaluationNotebook[], "ClosingSaveDialog" -> False] after saving to avoid the save prompt. $\endgroup$ Aug 16, 2012 at 8:41
  • $\begingroup$ @SimonWoods -- Great idea. I didn't know about the setting. Thanks. $\endgroup$
    – Jagra
    Aug 16, 2012 at 12:03
  • $\begingroup$ FrontEndTokenExecute["QuitKernel"] Didn't close the notebook... $\endgroup$
    – dearN
    Aug 16, 2012 at 14:23
  • 1
    $\begingroup$ Including the following SetOptions[EvaluationNotebook[], "ClosingSaveDialog" -> False] FrontEndTokenExecute["FrontEndQuit"]; still gave rise to the dialog box :( $\endgroup$
    – dearN
    Aug 16, 2012 at 14:24

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.