2012/05/02

Mathematica and SSH


DISCLAIMER: we strongly discourage usage of proprietary software, since it violates fundamental user freedoms.
 
To run Mathematica on a remote machine, e.g., via ssh

$ ssh -X user@machine.domain.com

it is useful to not execute commands using the GUI. In fact, if the network is not optimal (like Internet), the computation would be drastically slowed down. Instead, one should run a Mathematica package via command line.

The steps to evaluate a sequence of Mathematica cells using a script are as follows:
  1. write the program using the Mathematica notebook as usual;
  2. select a cell, copy the content as InputForm, and paste it into a text file;
  3. repeat the step above for all the cells;
  4. save the text file with .m extension.
Then the package can be evaluated as:

$ math -script file.m

Alternatively, one may add a line at the beginning of the script, which will look as follows:

#!/usr/local/bin/MathematicaScript -script

(* output a file containing Cos[x] *)
Export["output.dat",Table[{x,N[Cos[x]]},{x,0,Pi}]]

Then make executable the script:

$ chmod a+x script.m

and run it:

$ ./script.m

To keep the run also without keeping the shell open, use:

$ nohup ./script.m &


Refs: Mathematica Docs, Bergman Lab