0

I am working in a project which needs to get a list of PIDs with, for each process:

  • process name
  • process start time
  • memory and CPU usage

I am using Python for this project and I need it to work on both Windows and **ix.

I have done some research on SO (List running processes on 64-bit Windows) but that solution was specific to Windows OS. I don't know where to go next or which module to use.

Thanks in adv.

Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
  • @jonrsharpe, with this link(https://stackoverflow.com/questions/1632234/list-running-processes-on-64-bit-windows) I have got Pids and process name but it is specific to win OS. So, now I am not getting the answers of these question- 1.How to make is generic for all the os? 2. how to get start time, memory and cpu usage? Thanks –  Jan 31 '19 at 14:48
  • That's all very broad, I'd suggest you do some more research. – jonrsharpe Jan 31 '19 at 14:49
  • @jonrsharpe, could you please suggest me any specific module in which I can get solution easily. –  Jan 31 '19 at 14:52
  • Recommendations for external resources like modules, libraries, etc. are explicitly off topic here - see the [help]. – jonrsharpe Jan 31 '19 at 14:55
  • @jonrsharpe, I am really sorry. Are you here to demotivate someone? –  Jan 31 '19 at 14:57
  • @ManojKnitan Your linked question has an answer (https://stackoverflow.com/a/16519604/10622916) that refers to "psutils". Maybe you can use this. – Bodo Jan 31 '19 at 15:39
  • Like @Bodo said, `psutils` seem the right choice. You could check in particular this link https://github.com/giampaolo/psutil#process-management that seems to contain all the information you need. – Jean-Francois T. Feb 03 '19 at 05:42

1 Answers1

1

As mentionned previously, psutil seems the best tool to do what you need: https://github.com/giampaolo/psutil#process-management

Here is an example of how to retrieve the data:

from datetime import datetime
import psutil

# Getting the list of all processes (as a list, or with other attributes)
list_pids = psutil.pids()
for proc in psutil.process_iter(attrs=['pid', 'name', 'memory_percent']):
    print(proc.info)

print("===SINGLE PROCESS==")
try:
    notepad = subprocess.Popen("notepad.exe")
    pid = notepad.pid
    sleep(0.5)
    # We get a "Process" from the PID
    process = psutil.Process(pid)

    # We can then retrieve different information on the processs
    print(f"NAME: {process.name()}")
    print(f"ID: {process.pid}")
    print(f"STATUS: {process.status()}")
    print(f"STARTED: {datetime.fromtimestamp(process.create_time())}")
    print(f"CPU: {process.cpu_percent(interval=1.0)}%")
    print(f"MEMORY %: {process.memory_percent():.1f}%")

finally:
    notepad.kill()
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
  • Thanks for the nice explanation. It fulfills my requirement. –  Feb 04 '19 at 15:27
  • @ManojKnitan You can do a +1 if you like the answer. Cheers – Jean-Francois T. Feb 05 '19 at 09:25
  • Jean-Francois: Do you have any idea that how to take a snapshot of memory usage at any specific time(suppose 4:00 am). Thanks in adv. –  Feb 05 '19 at 11:18
  • @ManojKnitan memory usage of specific command or all memory used by system? – Jean-Francois T. Feb 05 '19 at 13:14
  • Jean: All memory used by system but it should be at specific time. –  Feb 05 '19 at 13:30
  • @ManojKnitan : you can combine this answer (https://stackoverflow.com/a/16786600/1603480) with usage of `psutil` to get the memory usage of whole system.... and don't forget to put a +1 on answers that help you (mine or others as well). Cheers – Jean-Francois T. Feb 05 '19 at 14:16