I run mathematica on a remote server and run it in batch mode using /math -noprompt -script 'scriptname.m' &

Now, I use ssh -X to connect to the server. The server is linux based and my computer is a mac. I use x11 forwarding.

I would like to generate PDFs in the above mentioned 'script.m'. In my code, how should I incorporate UseFrontEnd? should I invoke it first using ConnectToFrontEnd[] ? Should I use UseFrontEnd[Export["blah.pdf",fig]] or is it a one time thing? Also, when using the UseFrontEnd command, is it required that I do ssh -X/Y or is it sufficient if I do ssh?

I greatly appreciate your help.


P.S - I am using mathematica 8

  • 1
    $\begingroup$ this might help: superuser.com/questions/381807/… $\endgroup$ – au700 Nov 21 '12 at 18:42
  • $\begingroup$ not exactly. How do I use UseFrontEnd together with Export? and should I do that EVERYTIME I export a file to pdf? $\endgroup$ – preeti Nov 21 '12 at 18:49
  • $\begingroup$ stackoverflow.com/questions/315464/… this seems to help. But still, I don't know if I need to code Needs[Jlink`] and then UseFrontEnd[Export[]] everytime I export. $\endgroup$ – preeti Nov 21 '12 at 19:00

It is sufficient if you just do ssh - I was able to use the following commands without X11 forwarding.

After you ssh to the machine with Mathematica and invoke MathKernel you can do in the interactive session:

UFE = UsingFrontEnd
(* Out[4]= UsingFrontEnd *)
plot = UFE @ Plot[x^2,{x,-2,2}]
(* Out[5]= -Graphics- *)
UFE @ Export["/home/username/picture.jpg",plot]
(* Out[6]= /home/username/picture.jpg *)

And you can see the generated picture:

sample image generated in interactive session in MathKernel

The UFE = UsingFrontEnd line is to make the input lines shorter. Of course you could write plot = UsingFrontEnd @ Plot[x^2,{x,-2,2}].

After inspecting the output of monitorlm (Mathematica network license manager) I think that the first invocation of UsingFrontEnd starts a SubMathematica process and the same process is used in subsequent UsingFrontEnd commands. The SubMathematica process is terminated when the interactive MathKernel session is closed.

I'm sure things are very similar when using a .m script.

edit I am using Mathematica installed on Ubuntu 11.10 end edit

edit 2 After OP's comment I chcecked that to Plot[] and Export[] you don't need UsingFrontEnd. So the following works fine:

plot = Plot[x^2,{x,-2,2}]
(* Out[1]= -Graphics- *)
(* Out[2]= /home/username/picture.jpg *)

The reference says that UsingFrontEnd is necessary for functions like NotebookWrite[], when used from MathKernel. end edit 2

| improve this answer | |
  • $\begingroup$ this looks promising. I'll try. So you say I have to say UsingFrontEnd every time before I invoke some kind of plotting command or export graphics? Is there a way to just turn the switch on at the beginning of the script and turn it off at the end? $\endgroup$ – preeti Nov 21 '12 at 19:35
  • $\begingroup$ welll it seems you don't need the UsingFrontEnd command to plot = Plot[x^2,{x,-2,2}] and Export["/home/username/output.jpg",plot]. The reference says that UsingFrontEnd is neccesary for NotebookWrite[] and similar functions (I have used it to NotebookPrint[] some time ago). $\endgroup$ – au700 Nov 21 '12 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.