-2

Is there a way in python to hijack the terminal stdin? Unix only solutions will do fine.

I'm currently writing a small wrapper around top as I want to be able to monitor named processes, e.g. all running python instances. Basically I'm calling pgrep to get process id's and then runs top using the -p option.

Overall this script have worked satisfactorily for a few years now (well with the caveat that top -p only accepts 20 pid's...). However, I now would like adjust the script to update the call to top if new processes matching the name pattern are born. This also works relatively nicely, but... any options set interactively in top gets lost every time I update the pid list but natural causes as I stop and restart top. Therefore I would like to hijack the terminal stdin somehow to be able to backtrack what ever the settings are in affect so I can set them accordingly after updating the pid-list, or even halt updating if neccesary (e.g. if top is awaiting more instructions from the user).

Now perhaps what I'm trying to achieve is just silly and there are better ways to do it, if so I'd highly appreciate enlightenment

(oh. the tag ps were used as the tag top does not exists and I'm to new here to define new tags, after all the two utilities are related)

thanks \p

cpaitor
  • 423
  • 1
  • 3
  • 16
  • I'm having trouble parsing this. What interactive options are you talking about? I'm not sure what you mean by "backtrack". What are you trying to send over stdin? Perhaps post a log of what a you doing? Check out psutil https://pypi.python.org/pypi/psutil – RobertB Jan 31 '17 at 20:06
  • Hi Robert, perhaps you want to read up on the man page of `top` its not reall y – cpaitor Jan 31 '17 at 21:14
  • Not really what? I'm familiar with top. I'm just trying to read your post and it is confusing. – RobertB Jan 31 '17 at 21:18
  • Hi Robert, I accendentially managed to send the reply before being done writing it. For the interactive mode in top see section 4. in the man pages. By "backtrack" I simply mean "trying to find out the last characters written to the the terminals stdin". I'm quite aware of `psutils` but my intention here is not to rebuild top but rather to try to add a few features while maintaining its normal functionality. Not sure where you got the idea that I was trying write something to stdout. But then again perhaps my wording is just hard to digest, if so apologies and hope the above clarifies some – cpaitor Jan 31 '17 at 21:26

1 Answers1

0

What you are doing sounds like a bit of a hack. I would just write a Python script using psutil that does exactly what you want. Whatever information you are interested in, psutil should give it to you - and more.

Quick and dirty:

import psutil
import time    

while True:
    processes = [ p for p in psutil.process_iter() if 'python' in p.name() ]

    for p in processes:
        # print out whatever information interests you
        print(
                 p.pid, 
                 p.name(), 
                 p.cpu_percent(),
                 p.io_counters().read_bytes,
                 p.io_counters().write_bytes
                )
    time.sleep(10)

Link to Documentation: http://pythonhosted.org/psutil/

RobertB
  • 1,879
  • 10
  • 17