# Polling Unix Commands Top

How might I display an up to date display of top inside of Mathematica?

top is unique in that it updates itself every couple of seconds.

The following works for basic commands like ls but not top for some reason.

Import["!LD_LIBRARY_PATH= top", "Text"]


You need to use the proper flags with top. For example -b is for batch mode which is more appropriate for sending the output to another program. We are getting 1 frame (-n 1) before ending.

You can use something like the following:

Dynamic[RunProcess[{"top","-b", "-n 1"}, "StandardOutput"],
UpdateInterval->2, TrackedSymbols:>{}]


I added TrackedSymbols :> {} to lock the refreshes to the UpdateInterval.

This works:

process = StartProcess[\$SystemShell];
WriteLine[process, "top"];
Dynamic[ReadString[process, "Processes: "], UpdateInterval -> 1]


The reason you can't use Import is probably because the top process doesn't terminate. It's the same thing if you try to use ReadString without a second argument, it won't return anything because it will be waiting for the process to terminate first.

Since top will continuously fill the buffer we have to detect somehow when one table ends and a new begins. A new table begins with the word "Processes", so I used that.

• are you on mac or linux? – William Oct 26 '16 at 22:18
• @Liam Mac, if it doesn't work try ReadString[process, EndOfBuffer] and see if that returns something. – C. E. Oct 26 '16 at 22:28
• It freezes ubuntu so far for me. I will add a bounty in 2 days. – William Oct 26 '16 at 22:34
• @Liam If you run ReadString[process, EndOfBuffer] not inside Dynamic, but on its own, it freezes? – C. E. Oct 26 '16 at 22:48
• It freezes on and off. It isn't entirely unusable. If I run what you posted without or without ReadString[process, EndOfBuffer] it freezes on and off. – William Oct 26 '16 at 22:59