5
$\begingroup$

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

$\endgroup$
11
  • 1
    $\begingroup$ 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). $\endgroup$
    – N.J.Evans
    Jun 19, 2015 at 18:16
  • 1
    $\begingroup$ related: Export is not available during initialization .. mathematica.stackexchange.com/q/67632/2079 $\endgroup$
    – george2079
    Jun 19, 2015 at 19:08
  • $\begingroup$ If initialization was the issue wouldn't I still run into the same problem running the script from the command line? $\endgroup$ Jun 19, 2015 at 19:20
  • $\begingroup$ 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... $\endgroup$ Jun 19, 2015 at 19:31
  • $\begingroup$ 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... $\endgroup$ Jun 19, 2015 at 19:33

1 Answer 1

2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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