3
$\begingroup$

How can I schedule custom Mathematica code to run on my windows machine?

I need to schedule some code to run on my computer (Windows, Mathematica 10.0). I'm trying to get something to run via a command prompt so that I know I'm on the right track.

I'm under the impression that I wouldn't be able to run a .nb file from a command line, so I've written the following test code using 'Begin' and 'End', which I think makes it a package:

BeginPackage["mkTest`"]
Print["abc"]
a = {{1, 2, 3}, {7, 6, 5}};
Export[NotebookDirectory[] <> "\\test.xlsx", "testCmd" -> a];
End[]

I've placed this directly in the Mathematica folder:

"C:\Program Files\Wolfram Research\Mathematica\10.0"

The following is a screen print of various command line attempts:

screen print of my attempts

It seems the code isn't working or I'm not calling it properly, since no Excel file appears in my directory. Each of these attempts resulted in a window such as the following:

result of my attempt

  1. Is it necessary to write a package to run Mathematica code at the command line?

  2. Can anyone point out why my attempts above fail?

  3. How can I schedule custom Mathematica code to run on my windows machine?

$\endgroup$
3
  • $\begingroup$ On Windows, use math.exe., not MathKernel.exe. The former is a command line program, the latter is a graphical one. $\endgroup$
    – Szabolcs
    Commented Nov 12, 2014 at 22:04
  • $\begingroup$ @Szabolcs I tried your suggestion. When I didn't use -Script it gave me In[1]: on my command line. When I did use -Script I didn't have that problem, but I saw no evidence that cmdTest.m ran. I tried to create an Excel file or Print a string to give me some evidence, but nothing happened. $\endgroup$ Commented Nov 12, 2014 at 22:29
  • $\begingroup$ Actually I don't have Windows handy at the moment so I didn't test this. Sorry about the misleading information ... $\endgroup$
    – Szabolcs
    Commented Nov 12, 2014 at 22:40

1 Answer 1

3
$\begingroup$

From this answer: mathematica start front end and eval notebook from command line

This runs a .m file called first.m

cd C:\Program Files\Wolfram Research\Mathematica\7.0
MathKernel -noprompt -initfile firstgo.m

The answer also shows how to run it from a batch file which can be run by Windows Scheduler.

Additionally calling the front end as the example shows may be unnecessary for your purpose, in which case firstgo.m would contain your code.

$\endgroup$

Your Answer

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

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