# Running Mathematica Notebook files in command mode

The question is, how can I run a .nb file in the kernel mode of Mathematica? I am not an expert in Mathematica, but one of our users who use this program says that in the GUI mode, he selects all the cells (CTRL+A) and then evaluates the notebook (SHIFT+ENTER). However, he wants to run the program in background.

When I test with math < file.nb, the program quickly exits; however, in the GUI mode, the run time is very large actually.

I read other documentation articles about that, but since I am not expert in Mathmatica, I have no idea!

As an example, solve.nb file is an input to the command math -run < solve.nb. The output is also available here.

I have no idea what the output means :|

# Update

I simply tried to port the solution to Linux. So I wrote a solve.m file containing

NotebookPauseForEvaluation[nb_] := Module[{},
While[NotebookEvaluatingQ[nb],Pause[1]]];

NotebookEvaluatingQ[nb_]:=Module[{},
SelectionMove[nb,All,Notebook];
Or@@Map["Evaluating"/.#&,DeveloperCellInformation[nb]]
];

UsingFrontEnd[
nb = NotebookOpen["/home/mahmood/solve.nb"];
SelectionMove[nb, All, Notebook];
SelectionEvaluate[nb]
NotebookPauseForEvaluation[nb];
NotebookSave[nb];
];

Quit[];


Here is the output of what I see

mahmood@cluster:~$MathKernel -noprompt -initfile solve.m mahmood@cluster:~$
^C
mahmood@cluster:~$ Note that I pressec ^c after several minutes. Also, the is no output file containing th results. # Update 2 I tried the solution as given by selecting the cells, initialize them and then save the file as .m. I did that on a GUI machine. The saved script file contains (* ::Package:: *) a = 3; Solve[x^2 + a*x + 1 == 0, x]  As you can see, the last line in the notebook file is not there in the script file. I ran the command and saw mahmood@cluster:~$ /apps/Mathematica/10.3/SystemFiles/Kernel/Binaries/Linux-x86-64/MathematicaScript -script solve3.m
mahmood@cluster:~$ Is that all? There is no output file containing the result • Either use math - run < file.nb or math -script file.nb. If you are still in trouble in can post a detailed description of the steps needed to run a Wolfram Language program the way you want. – Sektor May 15 '16 at 18:37 • Make sure your cells are Code cells if you running a notebook. I general, if you are running in script mode I would recommend making .m (or .wl) files (File > New > Package). – chuy May 16 '16 at 14:08 • @mahmood - why do you need to use the notebook? I run scripts in command mode all the time, and every time what I do is copy the comands from the notebook into a text file and feed that to math – Jason B. May 20 '16 at 9:51 • Is there any way to convert a notebook file to script? It seems that the user who works with Mathematica tries the simplest way. Maybe he has complex expression and for that reason he chooses the .nb file – mahmood May 20 '16 at 12:35 • To convert the notebook to a script open the notebook, select the cells to be executed, on the menu do: Cell, Cell Properties, Initialization Cell. Then also on the menu do File, Save As... (file type) Wolfram Mathematica Package (.m). Also see Wolfram Language Scripts. – Chris Degnen May 20 '16 at 12:51 ## 2 Answers It is not a good idea to try to run a notebook in command line mode. Whether or not it is possible, it is just not a good idea because notebooks can only be handled by the Front End, which is not a command line tool. If you go this route anyway, expect difficulties. The usual way is to extract the code you want to run into a plain text .m file and run that. Do this by marking all cells to be exported as initialization cells. Select them, then choose Cell -> Cell Properties -> Initialization Cell. Or often simpler: put them under a section header and mark the header as "Initialization Group". Then use File -> Save As... -> Wolfram Mathematica Package. When running it as a script in command line mode, use MathematicaScript -script filename.m (for some reason math -script may not work the same, so don't use that). • There is no MathematicaScript. Files are math mathematica Mathematica MathKernel mcc wolfram WolframKernel. Is that important? – mahmood May 20 '16 at 13:57 • @mahmood What platform are you on? – Szabolcs May 20 '16 at 14:02 • Mathematica 10.3 on Centos-6.1 – mahmood May 20 '16 at 14:03 • @mahmood Then MathematicaScript should be there. Look again carefully and check in SystemFiles/Kernel/Binaries. It may not be in the path. You can also use WolframScript, which may be in the path instead. – Szabolcs May 20 '16 at 14:05 • @H.R. It is probably possible in recent versions using UsingFrontEnd[NotebookEvaluate[...]]. But if you are going to run remotely, I strongly recommend not using the FE. It's not that hard to make an .m file. Also, the FE requires an X server on Linux, so you may need to set up Xvfb if your server runs Linux. – Szabolcs May 31 '17 at 10:31 To replicate the Ctrl+A Enter execution behaviour automatically you can run Solve.nb from a package file, say runSolve.m, containing the code below. While Wolfram Language Scripts are straightforward, this method has the advantage that output can be saved in the notebook as if it was being run manually. To run a notebook and monitor evaluation simultaneously without clashes a second kernel, e.g. Kernel2, is required. It can be set up via the Evaluation menu, Kernel Configuration Options, Add, Kernel Name: Kernel2, OK. (DeveloperCellInformation usage thanks to Arnoud Buzing.) runSolve.m NotebookPauseForEvaluation[nb_] := Module[{}, While[NotebookEvaluatingQ[nb],Pause[.25]]]; NotebookEvaluatingQ[nb_]:=Module[{}, SelectionMove[nb,All,Notebook]; Or@@Map["Evaluating"/.#&,DeveloperCellInformation[nb]] ]; UsingFrontEnd[ nb = NotebookOpen["C:\\yourPath\\Solve.nb"]; SetOptions[nb, Evaluator -> "Kernel2"]; SelectionMove[nb, All, Notebook]; SelectionEvaluate[nb] NotebookPauseForEvaluation[nb]; NotebookSave[nb]; ]; Quit[];  This can then be run manually or from a batch file, runSolve.bat cd \Program Files\Wolfram Research\Mathematica\10.4 start MathKernel -noprompt -initfile "C:\yourPath\runSolve.m"  runSolve.bat @echo off setlocal PATH = C:\Program Files\Wolfram Research\Mathematica\10.4\;%PATH% echo Launching MathKernel start MathKernel -noprompt -initfile "C:\yourPath\runSolve.m" endlocal  Both these files can be plain text. Alternative version To run the notebook visibly so you can see it executing use this version. It has a minor warning message detailed here, but I prefer it since it is useful to see the evaluation progress. runSolve.m NotebookPauseForEvaluation[nb_] := Module[{}, While[NotebookEvaluatingQ[nb],Pause[.25]]]; NotebookEvaluatingQ[nb_]:=Module[{}, SelectionMove[nb,All,Notebook]; Or@@Map["Evaluating"/.#&,DeveloperCellInformation[nb]] ]; Needs["JLink"];$FrontEndLaunchCommand = FileNameJoin[
{\$InstallationDirectory, "Mathematica.exe"}];

UseFrontEnd[
nb = NotebookOpen["C:\\yourPath\\Solve.nb"];
SetOptions[nb, Evaluator -> "Kernel2"];
SelectionMove[nb, All, Notebook];
SelectionEvaluate[nb]
NotebookPauseForEvaluation[nb];
NotebookSave[nb];
];

Quit[];

• Excuse me. I am using LInux. Although I can alter the batch file to be bash friendly, the loop and ping section is not clear for me. – mahmood May 19 '16 at 14:29
• No. It doesn't work... I was trying hard to run it on linux. I am not an expert with Mathematica... As a cluster administrator, I was trying to help the users to be able to run their application via the terminal. Please see the updated post. – mahmood May 20 '16 at 9:21
• I have updated my answer. (No extra lines required in Solve.nb now.) – Chris Degnen May 20 '16 at 11:00
• Excuse me... I ran your script but my file name was solve.m and not Solve.m. I will update the answer soon – mahmood May 20 '16 at 12:27
• I ran it with math`. There is no output. Please look at the updated post which shows the notebook. Does that produce an output and the math command doesn't show that? I want to be sure that the command is successfully run. – mahmood May 20 '16 at 14:05