Message Boards Message Boards

Running Mathematica notebooks from linux command?

Posted 9 years ago

Hi, I am not an expert with Mathematica... As a cluster administrator, I am trying to help users to run their applications via the terminal.

A very simple notebook file (.nb) is here:

(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 10.3' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       158,          7]
NotebookDataLength[      1918,         77]
NotebookOptionsPosition[      1570,         60]
NotebookOutlinePosition[      1915,         75]
CellTagsIndexPosition[      1872,         72]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{
Cell[BoxData[
 RowBox[{
  RowBox[{"a", "=", "3"}], ";"}]], "Input",
 CellChangeTimes->{{3.67245858111902*^9, 3.6724586091944237`*^9}}],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"Solve", "[", 
  RowBox[{
   RowBox[{
    RowBox[{
     RowBox[{"x", "^", "2"}], "+", 
     RowBox[{"a", " ", "x"}], "+", "1"}], "==", "0"}], ",", "x"}], 
  "]"}]], "Input",
 CellChangeTimes->{{3.6724585110847526`*^9, 3.672458525710531*^9}, 
   3.672458572665449*^9}],

Cell[BoxData[
 RowBox[{"{", 
  RowBox[{
   RowBox[{"{", 
    RowBox[{"x", "\[Rule]", 
     RowBox[{
      FractionBox["1", "2"], " ", 
      RowBox[{"(", 
       RowBox[{
        RowBox[{"-", "3"}], "-", 
        SqrtBox["5"]}], ")"}]}]}], "}"}], ",", 
   RowBox[{"{", 
    RowBox[{"x", "\[Rule]", 
     RowBox[{
      FractionBox["1", "2"], " ", 
      RowBox[{"(", 
       RowBox[{
        RowBox[{"-", "3"}], "+", 
        SqrtBox["5"]}], ")"}]}]}], "}"}]}], "}"}]], "Output",
 CellChangeTimes->{{3.67245857460305*^9, 3.672458601115856*^9}}]
}, Open  ]]
},
WindowSize->{759, 601},
WindowMargins->{{Automatic, 295}, {24, Automatic}},
FrontEndVersion->"10.3 for Microsoft Windows (64-bit) (October 9, 2015)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[558, 20, 134, 3, 31, "Input"],
Cell[CellGroupData[{
Cell[717, 27, 290, 9, 31, "Input"],
Cell[1010, 38, 544, 19, 46, "Output"]
}, Open  ]]
}
]
*)

(* End of internal cache information *)

From the GUI, the user selects all cells (CTRL+A) and then evaluates cells (SHIFT+ENTER). Now, I wrote a simple .m file which contains.

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

and the command I run is

mahmood@cluster:~$ $MTH/Executables/WolframKernel -initfile solve.m
Mathematica 10.3.1 for Linux x86 (64-bit)
Copyright 1988-2015 Wolfram Research, Inc.

In[1]:=

However, it stuck and never returns to the command prompt Any idea about that?

POSTED BY: mahmood n
3 Replies

Someone else may be able to answer your question as it is, but I have to wonder: Can you just convince your user to write a proper script file instead of juggling notebooks?

Here's an example that reads data from an input file (you may or may not need that), does stuff, and exports it to an output file (two different files, in this case, just to show you some options):

input=Import["input.txt","Table"];
output=input*2;
Export["output.txt",output,"Table"];
Export["output.m",output];

Put that into a (plain text) file, name it e.g. mycode.m, and then run it with a command like the following:

math -run -noprompt < mycode.m

(That line would be executed in the console, or from some sort of start script - whatever you do usually.)

If the output has to be a Mathematica expression (and can't be just a plain text file), you can always simply export it to a package (.m) file and load that into an interactive notebook with Get. A text file has the advantage that it'll be more readable for humans and other programming languages, but it probably won't make too much sense if the results are more complex than a list or array of numbers. So that'll depend on the specific use case.

Oh, and if anyone's wondering, here is the actual code from the original notebook mahmood posted:

a = 3;
Solve[x^2 + a x + 1 == 0, x]
POSTED BY: Bianca Eifert
Posted 9 years ago

Thanks for that. Since the equations and expressions are complex, maybe he goes to the easiest way by writing notebooks instead of scripts.

Really, isn't there any way to run a notebook without GUI? Anyone who has some experience with that please notify us.

POSTED BY: mahmood n

He can take the exact same code he's running in the notebook and copy and paste it into a package file, he just has to add the line that exports the results. The formatting in the package file won't look pretty, but it'll work the same. The actual code development would still be done in a notebook, copying to the script would be the final step before starting the job. (The only problem that might occur is unwanted multiplication across line breaks, so tell him to put a semicolon at the end of each complete expression in the script version. He won't see the graphical output anyway.)

That said... yes, I suppose there must be a way to run a notebook file directly. Are you sure that UsingFrontEnd works in your setup at all? I mean, you do need to have an actual frontend installed for that, so you'd potentially have the GUI on that machine anyway.

POSTED BY: Bianca Eifert
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract