I want to run Mathematica "jobs" on a daily/weekly basis to do system maintenance and as part of daily processing operations. What is the best way to do this?
This turns out to be easy. In Windows 7:
- Add C:\Program Files\Wolfram Research\Mathematica\9.0\ to the path.
- Write procedures for what you want to do in The Wolfram Language, and save in a .m file.
- Add MathKernel.exe -script filename.m to the Windows Task Scheduler. Specify that the job runs in the directory containing the .m file.
That seems to be it. by using MathKernel.exe instead of Mathematica.exe you avoid problems related to Initialization Cells. By using the -script flag you tell MathKernel to end the job after executing the last line of code (evaluating the last function).
There has been some discussion on SE about the meaning/value/purpose of The Wolfram Language - it's a marketing ploy, etc. Here's what is especially interesting about my little question to me - it's an example of using The Wolfram Language without the Mathematica interface. People seem to confuse (IMHO) The Wolfram Language with Mathematica. So here it is: put code in .m files; run them with the MathKernel - you're using The Wolfram Language.
And, initially surprisingly to me, I don't need to invoke MathKernel directly to run the script. If, in a command line window, I moved to the folder holding the .m file, I can run the script just by typing filename.m on the command line. I'm calling it a script, but in fact it could be a very large program, so perhaps the term script obscures the fact that Wolfram Language programs don't require Mathematica.
I have recently found numerous references to MathKernel, scripts, and related topics in official Mathematica documentation that I didn't know about before. I'll list some of them:
Perhaps in the future we will see more discussion about running Wolfram Language programs in the Mathematica MathKernel. If anyone has opinions about this, please comment.