# Evaluate multiple notebooks sequentially and automatically

I know how to run multiple notebooks at once. But my problem is that due to the RAM capacity, I have to run these notebooks consecutively. Another problem is that since I may not be in my office all the time, I want these notebooks to be run automatically. For example, notebook1 is evaluated, then notebook2 is evaluated, and so on.

Is there any way to do this? Any idea would be appreciated.

• see NotebookEvaluate – george2079 Sep 8 '15 at 18:45
• Thanks, but I don't want to evaluate notebooks with in a kernel. Actually, I have 4 notebooks in 4 separate kernels, and I want to evaluate them sequentially. – Alex Sep 8 '15 at 18:59
• How would you know (or the other kernels, for that matter) know when an evaluation has finished ? – Sektor Sep 8 '15 at 20:15
• I suspect the best approach is to use a shell script for the looping and command line launch mathematica to eval each notebook. Saving will be a trick. Is it feasable to put some save code in each notebook? – george2079 Sep 8 '15 at 21:13

If you really need to do everything in the frontend, then I would Suggest the below procedure, in this process Mathematica would open a new window selects all the cells and runs them then it would go for the second notebook file

nb1 = NotebookOpen["/Users/you/Documents/1.nb"];
SelectionMove[nb1, All, Notebook]
SelectionEvaluate[nb1]
nb1 = NotebookOpen["/Users/you/Documents/2.nb"];
SelectionMove[nb1, All, Notebook]
SelectionEvaluate[nb1]


Hope this will help

Second Approach

If you are using OS X you can use the shell script

first give the path to the Mathematica then run the kernel to run your scripts remember you need to save them as .m files as save as option wont work alone you have to convert them to standard form cmd+8 in OS X if I'm not wrong.

math=/Applications/Mathematica.app/Contents/MacOS/MathKernel

"$math" -script 1.m "$math" -script 2.m


And you need complete path to .m files by the way!

• Thanks, but the first approach runs two notebooks at the same time. While, as I mentioned before, I want them to be evaluated sequentially. – Alex Sep 10 '15 at 0:38
• you are mistaken! It does open them simultaneously, but starts running the cells one by one! Starting from the first opened notebook file add the command Pause[10] in to first notebook and watch. The second notebook file is open but it's waiting for the first notebook to be evaluated. To be sure I checked again. Good luck – Sarah Aria Sep 16 '15 at 0:34
• well as sarah said, you are wrong. – Raymond Ghaffarian Shirazi Sep 19 '15 at 23:34
• Thank you @RaymondGhaffarianShirazi and @Sarah! I have another question. I have 24 runs that I'd like to allocate a maximum time (e.g., 2 hours) to each run. So, I'll need 48 hours, but if I distribute these 24 runs among 4 kernels, the whole computations can be done in 12 hours. How can I do that? Any help/idea would be appreciated! – Alex Oct 9 '15 at 1:04