5

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??

  • 1
    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). – N.J.Evans Jun 19 '15 at 18:16
  • 1
    related: Export is not available during initialization .. http://mathematica.stackexchange.com/q/67632/2079 – george2079 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? – TheBlindSpring 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... – Albert Retey 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... – Albert Retey Jun 19 '15 at 19:33
  • When I run it from LXTerminal it works properly. – TheBlindSpring Jun 19 '15 at 19:53
  • is UseFrontEnd necessary? – george2079 Jun 19 '15 at 22:21
  • I was thinking about a real text console (what you typically get when pressing Ctrl-Alt-F1 on linux), not an emulation which runs as a window in a graphical environment. The point is that there you want no X server running that the script could access. When running from LXTerminal (or any other terminal emulation which is an X client) an X server be available. (note that you should add a @AlbertRetey when writing a comment to be addressed to me, otherwise I will not see it, see the small help link right to the comment field...) – Albert Retey Jun 20 '15 at 13:14
  • I think it could also be good enough to unset the $DISPLAY environment variable, but I have no possibility to test that at the moment... – Albert Retey Jun 20 '15 at 13:22
  • @AlbertRetey found the problem in this particular instance (Feel free to answer). The issue was I don't know anything about cron and in my cron setup is have pi sudo which makes no sense but I didn't catch it at the time. – TheBlindSpring Jun 24 '15 at 19:27
  • so if you throw away the sudo it works even with UseFrontEnd? Interesting but good to known. As for the answer: it is welcomed to self-answer on this site in cases like that, I'm happy if you do that. – Albert Retey Jun 24 '15 at 20:45

1 Answers1

2

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

Bad Code:

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.