# Wolfram Script Not Completing when ran as cron job

I have a fairly simple Wolfram script called from within a shell script that I would like to run at certain times. The shell script calls a python script to get some recordings to from GPIO and saves these results as file. The Wolfram script opens this file, plots the data, exports the plot as a .jpg, then emails me the jpg. Here is the Wolfram script:

data = Import["./ADayRecord.txt", {"Data", {All, {1,2}}}]

dataMean = Mean[data]

Print[ dataMean[[2]] ]

ConnectToFrontEnd[]

UseFrontEnd[ Export["./SensorScript.jpg", ListLinePlot[ {data} ] ] ]

CloseFrontEnd[]

Print[DONE]


So the problem I am having is that I would like to automatically run this (shell) script using the unix cron utility. If I run the script from the shell terminal, everything works perfectly. I can see this because I am redirecting the output of the Wolfram script to a file and I see it printing "DONE" and I see the SensorScript.jpg. However, now I try running the script through cron with 17 * * * * pi sudo /home/pi/monitorScript.sh (17 was just the last time i tested it, I change it every time I try to test it). The scripts starts, running the Python code as it should, then it goes to the the Wolfram script and it seems to get stuck on export (I tried adding UseFrontEnd, but to no avail). I go to the output file of the Wolfram script and I see it printing the mean, but I never see "DONE". Also, when I run the script from the shell terminal, WolframKernel closes once the script finishes. When cron runs the script, WolframKernel never closes (probably because its stuck on export for some reason).

I am leaning towards something being wrong with the Wolfram script, but it could very well be a problem with cron, so I apologize if this is the wrong platform for this question, I am just kind of at a loss. I know paths need to be set up properly when running things from cron, but it is able to read "./ADayRecord.txt" and get the correct mean for it (from cron), so I don't think paths are the issue. Why would this run normally from the terminal and hang when running from cron??

• Have you tried providing complete paths to Import and Export? Funny stuff sometimes happens with things like ./ or env variables when you're running scripts from scripts. (I see that you noticed paths might be a problem, that's where I'd start). Jun 19 '15 at 18:16
• related: Export is not available during initialization .. mathematica.stackexchange.com/q/67632/2079 Jun 19 '15 at 19:08
• If initialization was the issue wouldn't I still run into the same problem running the script from the command line? Jun 19 '15 at 19:20
• have you tried running your script on the console as root or with sudo? Setting up a cron job to be started as user pi and then use sudo in the command seems a bit odd, is there a good reason why you do that? Other than that, for UserFrontEnd to work I think you would need to make available an X server to the cron job (at least thats what you have to do for webMathematica), see documentation here but that seems a little overdressed for your use case... Jun 19 '15 at 19:31
• you might want to test your script directly on a text console to see/verify that the missing X access really is why Export doesn't work... Jun 19 '15 at 19:33

So I had a couple of issues with my code, the first as AlbertRetey pointed out, was how I set up my cron job.

Bad Code: 17 * * * * pi sudo /home/pi/monitorScript.sh

Corrected Code: 17 * * * * pi /home/pi/monitorScript.sh

data = Import["./ADayRecord.txt", {"Data", {All, {1,2}}}]

dataMean = Mean[data]

Print[ dataMean[[2]] ]

ConnectToFrontEnd[]

UseFrontEnd[ Export["./SensorScript.jpg", ListLinePlot[ {data} ] ] ]

CloseFrontEnd[]

Print[DONE]


Corrected Code:

data = Import["./ADayRecord.txt", {"Data", {All, {1,2}}}]

dataMean = Mean[data]

Print[ dataMean[[2]] ]

Export["./SensorScript.jpg", ListLinePlot[ {data} ] ]

Print[DONE]


I will also mention that when I removed everything regarding FrontEnd, it still wasn't working and I believe it is because I was starting Wolfram with the -noprompt option and when I removed it, it started to work (exported .jpg). Someone feel free to correct this last statement if it is incorrect.