skip to content
 

Running Mathematica Batch-files in the Background

In order to run a Mathematica program in background, first you need to create a batch-file which is a text file that contains all the input commands that are written in different cells of a Mathematica Notebook. For this purpose, you must do the following:

  • Select the cells from the Mathematica Notebook, and then follow the direction by clicking on Cell > Cell Properties >Initialization Cell from the menu bar to initialize the cells.
  • To generate the batch-file, follow the direction by clicking on File > Save As Special. . . > Package Format from the menu bar.

Then a dialog box appears for specifying the file name and the location of the mathematica input file. Supose you save it with the name 'mycommands.m'. By using the Mathematica kernel alone, you can then run this file as a background job:

nohup time math < mycommands.m > myresults.out &

A 'myresults.out' file will be created containing all the output from the computations. If you wish to have both the output results and the input commands in that file, you need to add at the beginning of the file 'mycommands.m' the following (you can do this by using a text editor like Vim or Emacs),

AppendTo[$Echo, "stdout"]

If you wish to keep track of the computations, then probably the best way is to do them interactively by using the mathematica kernel inside a GNU screen session, that you can detach and attach via ssh from other location.