# Keep MathKernel Running In Background - Speed Up Execution Time

Background: I'm running Debian on a remote server with Mathematica installed. I have several dozen small mathematica files that are run in cron jobs at unpredictable intervals and updated based on a web interface. I'm trying to get faster response times when I run the smaller files.

Question: For Example I have file.m which contains(This is just an example)

Export["/home/rob/Desktop/eq.png",Sum[1/2,{x,i}]]


If I setup a cron job to run the following

time MathKernel -noprompt -script /home/rob/Desktop/file.m


I get a real response time of 6.31.

But now if I ssh into the server and run MathKernel and the following

AbsoluteTiming[Export["/home/rob/Desktop/eq.png",HoldForm[Sum[1/2,{x,i}]]]


the script returns 1.13.

The start up time definitely appears to be the issue 6.31 vs 1.13.

So what is the best way to keep MathKernel running it the background so it can periodically run file.m? I would like to avoid using CreateScheduledTask because of the extensive rewrite that would be needed.

EDIT:This and this are clunky but should work.

-
Related: 41742 –  shrx Aug 5 at 8:17

Spawn1701D's idea to use $IgnoreEOF is interesting, but unfortunately this flag doesn't work at all in version 9, and it doesn't work when reading from files in version 8. Instead we can use the tail Unix utility to prevent Mathematica from stopping to read input. 1. Create a pipe: mkfifo mmpipe 2. Execute tail -f mmpipe | math. tail will keep feeding Mathematica from whatever is written to mmpipe. 3. Pipe input to mmpipe, e.g. echo 1+1 > mmpipe. Mathematica will execute it. You can do this as many times as needed. Just make sure that every input you give Mathematica is complete and correct. An unclosed bracket could make this approach fail. A more robust solution would use MathLink to feed expressions to Mathematica (this is what the Front End does). This would take more work to implement. EDIT: You'll find some more information about this method and its shortcomings in this thread. - Wouldn't it make more sense to use cat mmpipe instead of tail -f mmpipe? I was under the impression tail prints on the last 10 lines of its input. Also do you know of any way to the simultaneously write the mathematica repl input and output to maybe a log file? Although MathkLink is probably the best solution, but it would nice to have a way to view past commands and maybe errors to run commands in a log file. – William Apr 14 '13 at 18:32 @Liam Look up the -f option to tail. This is the essential bit that keeps the system from stopping as soon as the pipe is out of data. You'd have to test if it truncates though ... I'm not too good with Unix commands so I can't answer your second question, but take a look at tee. – Szabolcs Apr 14 '13 at 18:55 You have to set the variable $IgnoreEOF to True. Actually as the first job you can give the command

MathKernel -noprompt -script /home/rob/Desktop/init.m


where in init.m you have the command

\$IgnoreEOF = True

-
Keep in mind that you have to give an explicit Quit now to kill the MathKernel. –  Spawn1701D Apr 13 '13 at 7:38
So let's say I have file2.m. How would I take advantage of the already running MathKernel instance? It appears to just never end now. –  William Apr 13 '13 at 7:41
@Liam You mean that if you give MathKenel -run "Quit[]" it doesn't quit? –  Spawn1701D Apr 13 '13 at 7:52
@Liam You are right the next time you run MathKernel it opens a new process. Put how about using the CreateScheduledTask command to emulate the function of cron? With IgnoreEOF = True the kernel will not exit after reading the initial file with the scheduled tasks and at random times the files you want will be run be the scheduler. –  Spawn1701D Apr 13 '13 at 8:37
@Spawn170D I had thought about using CreateScheduledTask before posting the question, but I really don't think it would be practical. The cron jobs are modified on a fairly regular basis. Additionally I have had mathematica force quit before. The entire automation of the system would go down if mathematica dies once. To help others, I'm going to edit the question to exclude CreateScheduledTask as a possibility. –  William Apr 13 '13 at 8:51