# How to export graphics to SVG in script executed through cron?

I have a Wolfram script that generates plots, and exports them by evaluating expressions of the form

Export["/path/to/some.svg", myPlot];


The script works as intended when I run it from the [Unix] command line. In particular, the plots get exported to files in valid SVG format, AND no windows ever pop up.

And yet, if I run the same script through a cron-initiated command, it fails at the first such Export expression, with the error

Can't open display: ":0.0"


I.e., the script seems to be requiring a graphic display, even though it is operating non-interactively.

Is there a way around this?

FWIW, for this I'm using Mathematica 11.0.1 (it's what's available in my current employer's system).

Also, FWIW, the script gets invoked with

wolframscript -script /path/to/my/script.m


I can reproduce the error from the command line by running

DISPLAY= wolframscript -script /path/to/my/script.m


BTW, the solution given in Wolfram Script Not Completing when ran as cron job does not apply to my case, since my script uses none of ConnectToFrontEnd, UseFrontEnd, CloseFrontEnd, etc. In fact, my script makes no explicit mention of a front end anywhere.

• possible duplicate: mathematica.stackexchange.com/q/136574/9490 – Jason B. Dec 13 '17 at 20:33
• Export for some formats needs a frontend. Looks very much like SVG is among those formats, thus the error message. Thus I am pretty confident that your question is in fact a duplicate as Jason B. suggests... – Albert Retey Dec 13 '17 at 21:31
• I cannot reproduce, Linux 4.13.0-1-amd64 #1 SMP Debian 4.13.13-1 (2017-11-16) x86_64 GNU/Linux, Mathematica version 11.2. – Picaud Vincent Dec 18 '17 at 10:19

I can somewhat reproduce this error. Except for me, if I try to export a graphic with no display set, the kernel just hangs. The answer is to use an Xvfb display server, as described here.

I have the file, exportSVG.m, with the contents

plot = Plot[x, {x, -3, 3}];
Export["/home/jasonb/some.svg", plot];


Then another file, exportSVG.sh,

#!/usr/bin/env bash

xvfb-run /usr/local/bin/wolframscript -script /home/jasonb/exportSVG.m


If I add this to my crontab with out the xvfb-run, it hangs. If I add in that command, it works. You'll need to ensure you have it installed, check via which xvfb-run.

• Thanks for the suggestion. I'm still trying to get xvfb-run installed so that I can test your suggestion. (I have no super-user privileges on this system.) – kjo Dec 14 '17 at 23:19
• This thread may be helpful. Another option, if you can't rasterize remotely, is to export Mathematica Graphics expressions directly to a .m file (or .mx). Then you can open those output files locally and rasterize them on a machine with a DISPLAY available. – Jason B. Dec 17 '17 at 15:33
• Jason: I am sorry that you did not get the full bounty; this was ultimately my mistake, out of not entirely understanding the mechanics of the bounty system. If there's a way that I can supplement the bounty to bring it up to the originally offered value, please let me know. – kjo Dec 21 '17 at 18:00

Here is how I used it in version 6. On my recent 10.3 it still works: You can execute lines below directly in kernel (in text console, i. e. run math) or put it in .m file and use as script. Graphics server needs to be installed on the server.

whereIam="/home/acus/temp";
$Path=Append[$Path,whereIam];
SetDirectory[whereIam];

Needs["JLink"];

(* print in file if connection was succesful *)
arprisijungiau=ConnectToFrontEnd[];
Put[arprisijungiau,"testNon.txt"]

(* the computer were script runs *)
hostname=Import["!echo /bin/hostname","Text"];
PutAppend[hostname,"testNon.txt"]

(* now make real graphics *)
thePlot=Plot[Sin[x],{x,-Pi,Pi}];
SetDirectory[whereIam];

Export["some.svg", thePlot];

• I would expect this to fail when you are not logged on to that computer. Either :1 might not exist or you could have no access to it. If the script is supposed to reliably work from a cron job, I think it needs to use a frame buffer as Jason B. suggests. This is also documented in the webmathematica and the workbench documentations, search for Xvfb there. – Albert Retey Dec 13 '17 at 21:25
• Not sure, but I think it is nothing to do with logging. As far as concerns :1, you are right, on large system it can be used, but you can choose quite large number, say :13. In my computations I always prefered true graphics drivers. In fact webMathematica requires X server installed and frame buffer is in my opinion worse choice (I configured myself webMathematica, mokslasplius.lt/eksperimentai/isbandykite-webmathematica). In fact some time ago I used the solution to generate quite large number of graphics by automatic scripts. Don't see a reason why it should not work from cron. – user18792 Dec 14 '17 at 8:25
• what I try to say is that if you use a display setting, there needs a X server be running as the given display number and the process of the cron job needs to be authorized to use that display. Just giving -display :1` as an argument will not make that work if there is no X server running for that display number or the cron job is not authorized to use it. Looks like you have set up your machine accordingly, but it is essential information that the OP would have to do that for your answer to work for him... – Albert Retey Dec 14 '17 at 10:13
• I assume cron is running with user rights. So no additional authorization is required. X server is not need running for particular display. The display just should not be used by different user (in that way it will most probably not connect). Also the X server can be virtual, for example tightvnc, which I usually use. – user18792 Dec 14 '17 at 11:51