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??
Import
andExport
? 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). $\endgroup$Export
is not available during initialization .. mathematica.stackexchange.com/q/67632/2079 $\endgroup$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... $\endgroup$Export
doesn't work... $\endgroup$