3
$\begingroup$

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"]
$\endgroup$

2 Answers 2

3
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ are you on mac or linux? $\endgroup$
    – William
    Oct 26, 2016 at 22:18
  • $\begingroup$ @Liam Mac, if it doesn't work try ReadString[process, EndOfBuffer] and see if that returns something. $\endgroup$
    – C. E.
    Oct 26, 2016 at 22:28
  • $\begingroup$ It freezes ubuntu so far for me. I will add a bounty in 2 days. $\endgroup$
    – William
    Oct 26, 2016 at 22:34
  • $\begingroup$ @Liam If you run ReadString[process, EndOfBuffer] not inside Dynamic, but on its own, it freezes? $\endgroup$
    – C. E.
    Oct 26, 2016 at 22:48
  • $\begingroup$ 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. $\endgroup$
    – William
    Oct 26, 2016 at 22:59

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.