Mathematica is a great computational tool, and the Notebook interface makes it an almost self-contained system for doing calculations and documenting your work. However, there are some situations where one does not need or even want the interactive Notebook interface. For example, you may want to do a quick calculation while doing some work in the Terminal, or run Mathematica remotely without access to the graphical desktop.
The solution for this is to invoke an instance of the Mathematica Kernel from the command line. How you do this depends on where you are. I've been using this access method on various UNIX and Mac OS X systems at least since 1998, and it has worked with all versions of Mathematica so far.
math, the Kernel will start up and you'll see the following:
acad-cl1> math Mathematica 6.0 for Linux x86 (64-bit) Copyright 1988-2007 Wolfram Research, Inc. In[1]:=If you don't see this, type
echo $PATH and make sure that the directory path to the math command is in the list. On acad-cl, the path needed is /usr/local/bin/math.
On a Mac running OS X, Mathematica is installed in the Applications folder, e.g., under the name Mathematica. Assuming this is the case on your system, you can invoke the Kernel from the command line by typing
"/Applications/Mathematica.app/Contents/MacOS/MathKernel"
If you're on an Intel 64-bit machine, you may have to execute MathKernel64 here (depending on your Mathematica version). In fact, you may want to follow the instructions at
Wolfram's Mac support FAQ if it applies to your processor.
Obviously, if you do this kind of thing often, you'll want to define an alias such as
alias math "/Applications/Mathematica.app/Contents/MacOS/MathKernel"(in csh syntax). That way, the command
math will work the same way on UNIX and Mac OS X machines.
Needless to say, this is one of the great advantages of working with UNIX, Linux or Mac OS X: you can in principle start as many copies of your Mathematica Kernel as you want, even while the "proper" Notebook Mathematica is running.
<<JavaGraphics`
Plot[{BesselJ[0,x],BesselJ[1,x]},{x,0,1},PlotStyle->{Hue[.5],Hue[1]}]
Although this is a bit slow because it involves JLink (Java), it provides graphical feedback for your Kernel calculation without the need to open a notebook.
.m (e.g., foo.m by typing <<foo.m. Here, foo.m contains Mathematica commands such as Print["Hello World"].
math -run "<<input"The
-run option causes the command in quotation marks to be executed by the Kernel. Here, the command is to load the file named input. It does not matter whether that file has the extension .m or not, but its contents have to be Mathematica statements. We will use this below to start a batch job.
Turning to the contents of the command file, there is the problem that we need some way of receiving output from the process. Textual output will be displayed directly in your terminal, but graphics output will create error messages if the Kernel does not know where to send the graphics. Here is an example for the file named "input" that shows how this problem is solved:
SetOptions[Plot,DisplayFunction->Identity]
a=Plot[Cos[2*x],{x,0,Pi}]
Export["a.eps",a]
You can of course also enter these same commands interactively to see what they do. The first line has to be given only once, and it makes sure that all subsequent Plot commands produce no direct output. Instead, the output is stored in a variable a. The last line generates an Encapsulated Postscript file in you current working directory, named a.eps.
Although the above still works with Mathematica version 6 on Mac OS X, you may have to modify the procedure slightly when logged in through a terminal session. In that case, you may get the error
Export::nofe: A front end is not available; export of EPS
requires a front end.
The quick fix for this is to leave out the SetOptions line and export the expression in a as a text file: Export["a.m",a,"TEXT"]. The resulting file a.m can then be displayed in Mathematica's Front End using <<a.m, from where one can finally proceed to export other graphics formats such as EPS.
Having discussed the creation of command files, the last step is to make it possible to run command files over night, or longer, without having to stay logged in on the server. The first step is to modify the above to
math -noprompt -run "<<input"What this does is to tell Mathematica not to show the interactive command prompt discussed earlier, but to execute silently all the commands contained in the file named "input". In the absence of a prompt, it is essential that the input file terminates the Kernel when finished. This is ensured by making the last line of the input file read
Exit[] (or equivalently: Exit). Here is an example that mindlessly generates a list of twenty identical plots, just so we have time to log out before the execution finishes; save it as, e.g., test.m (the file name is arbitrary):
Print["Hello, starting plots!"]
SetOptions[DensityPlot,DisplayFunction->Identity]
a=Table[DensityPlot[Sin[Sqrt[x^2 + y^2]]^2/(.001 + x^2 + y^2), {x, -13, 13}, {y, -13, 13}\
, Mesh -> False, PlotPoints -> 300], {i, 1, 20}]
Do[Export["a"<>ToString[i]<>".eps",a[[i]]],{i,1,20}]
Exit[]
Now to launch this program in a way that does not get killed when you log out, type the following:nohup nice math -noprompt -run "<<test.m" > output &output catches any text output that your job might generate.