Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question
    
Although you reasked your question, I believe this is a duplicate as well. It just seems that you were unaware that one could run Mathematica from the command line without a FrontEnd. This is not something new to this whole "Wolfram Language" nonsense, but I believe has existed since version 1 (someone familiar with v1 can correct me if I'm wrong). If you were to actually focus your question and answer on the "doing system maintenance with Mathematica" part (with examples of scripts to do some common chores), then the question would be different and it can be reopened. –  rm -rf Dec 16 '13 at 21:59
    
I don't think it's the same question. None of the answers to my original question explained how to do what I tried to ask about. If the question was trivial, and the answer was obvious, someone would have said so, and answered it. But they didn't. –  George Wolfe Dec 16 '13 at 23:45
    
I was really interested in the mechanics of getting a Mathematica script to run in the background. I had never used MathKernel, so I didn't know how to do it. I didn't have any questions about how to do the system maintenance part. –  George Wolfe Dec 16 '13 at 23:48
    
I do see that Verbeia provided an answer in October to the question I asked in June, but I didn't see her answer in the interim. So, I do see your point about a duplicate question. –  George Wolfe Dec 17 '13 at 0:03
    
Hmmm... In your first question, it wasn't clear that you were referring to MathKernel, since you asked "How do I set up the notebook/file so it will execute when started without asking any questions?", hinting at a notebook approach (which the linked dupe addresses). As for this one, the link to Jens' website in bill s' answer to your first question addresses this. There he explains how to run scripts as a batch job, which was exactly what you were looking for. The only additional part is about adding it to the scheduler/cron, which you already knew about and is not related to Mathematica. –  rm -rf Dec 17 '13 at 0:38
show 2 more comments

marked as duplicate by rm -rf Dec 16 '13 at 21:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 8 down vote accepted

This turns out to be easy. In Windows 7:

  1. Add C:\Program Files\Wolfram Research\Mathematica\9.0\ to the path.
  2. Write procedures for what you want to do in The Wolfram Language, and save in a .m file.
  3. 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:

  1. A list of "new in 8" scripting features.
  2. A Mathematica script tutorial.
  3. A MathKernel guide.

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.

share|improve this answer
1  
Any bets that v10 will officially replace MatheKernel.exe with wolfram.exe? –  bobthechemist Dec 16 '13 at 13:10
1  
Can't really agree with this.."Wolfram Language programs don't require Mathematica" Should be "Wolfram Language programs don't require the Mathematica Front End" .. –  george2079 Dec 16 '13 at 21:07
1  
It is true as george2079 says, that though this might be practically useful to someone the philosophical aspect is a bit deceiving. I wasn't around, but I bet you could send scripts to the kernel in v.1. It's so basic - and all other interpreted languages allow it - that it must have preceded the graphical interface. –  Pickett Dec 16 '13 at 21:48
    
You could always directly access the kernel. IIRC, until recently thre was no -script option to let you nicely run in a batch mode. –  george2079 Dec 16 '13 at 22:20
    
If you started with Version 7 (like I did), you saw almost no discussion about the kernel, so this was something of a discovery for me. –  George Wolfe Dec 16 '13 at 23:53
add comment

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