vendredi 13 mars 2015

How do you get the 'top' command to work in Java runtime to return a list of processes and the amount of resources each are consuming?



This is kind of a newb question, so please forgive me, I am a newb. Before posting I have checked all x billion pages on the Internet and there is not answer to my specific question. Okay not really, but it's been a good four hours of desk punching so I feel it is a good time to defer to the vast and seemingly all encompassing knowledge of the SE user base. So, without further ado... here is my question.


As stated above, I am working with bash (OSX) and Java, and trying to get a script to run via process-builder (and/or Java runtime) IN THE EXACT SAME WAY that it would run by running it in the shell alone.


for example, if I write a shell script (.sh) containg the code:



"#!/bin/bash"

"top > /Users/NN2/Desktop/test.txt"



the file runs fine, and the expected output is created. However, if I do the following:



(excluded try/catch in example only)

ProcessBuilder builder = new ProcessBuilder(new String[]{"/bin/sh","/Users/NN2/Desktop/configure.sh"});


or


Runtime.getRuntime().exec("/Users/NN2/Desktop/configure.sh");



Either nothing happens, or a truncated version of the original appears, which does not include the #MREG info. In addition to the above two examples, I have tried what seems like an exhaustive list of other variations and it is always the same, either truncated or nothing. Furthermore, this problem seems to only be with the "top" command, as most other commands I have tried do indeed work (ex: ls, ef aux, etc..).


I have read that other people have had permission issues with the top command from within Java, however, I have used the chmod command on several other installations so this is likely not the cause of the problem either. However, if it is, then it is surely due to not knowing which files need to be chmod'ed.


Also, if there is any other way to find the information I need besides what I am doing please do tell. In the end, I just want to be able to use java to check what process are running on my computer, and how much memory they have used (to tell when a program has finished loading) before I do other things. Different file sizes open at different durations, so thread.sleep command is a work-around that I do not wish to use.


Thank you




Aucun commentaire:

Enregistrer un commentaire