-1

I would like to know how I can count how many instances of a program are running in the system using python.

For example: if my terminal is open 3 "times", and there are 3 instances of the terminal, how could I count them using python ?

Thanks in advance

tenacity
  • 456
  • 1
  • 5
  • 14
Meaks
  • 11
  • 2
  • I mean, if you're on Linux you can just use a Linux command to do it, like [this one](https://stackoverflow.com/questions/3058137/finding-process-count-in-linux-via-command-line), and then bring the results into Python; are you saying you'd rather invent your own way of getting the count of process/command instances? Why? – Random Davis Aug 10 '22 at 20:32
  • 1
    You could potentially use [psutil](https://pypi.org/project/psutil/) to get processes and the names of the executing programs. – tdelaney Aug 10 '22 at 20:32

1 Answers1

1

This problem is fairly very straightforward if one uses the psutil package in python3

Consider the code snippet below.

import psutil
from collections import Counter

process_ctr = Counter()

for process in psutil.process_iter():
    try:
        process_cmd_list = process.cmdline()
        for p in process_cmd_list:
            process_ctr[p] += 1
    except Exception:
        continue

for pname, cnt in process_ctr.items():
    print(pname, cnt)

Upon running this code, one will get the process name and count printed to stdout

Testing: I opened 3 terminals and I noticed this in the O/P: -bash 3

Sample O/P:

--pdf-renderer 2
--js-flags=--jitless 2
--renderer-client-id=657 1
--launch-time-ticks=75930041264 1
--renderer-client-id=658 1
--launch-time-ticks=76065123981 1
--seatbelt-client=151 4
--renderer-client-id=659 1
--launch-time-ticks=76065777110 1
--renderer-client-id=663 1
--launch-time-ticks=76077686392 1
--renderer-client-id=100 1
--launch-time-ticks=1656826210 1
--renderer-client-id=665 1
--launch-time-ticks=76250675532 1
--seatbelt-client=168 11
--renderer-client-id=666 1
--launch-time-ticks=76252057630 1
-bash 3
/System/Library/PrivateFrameworks/CoreFollowUp.framework/Versions/A/Support/followupd 1
/System/Library/PrivateFrameworks/PhotoAnalysis.framework/Versions/A/Support/photoanalysisd 1
/System/Library/CoreServices/ScopedBookmarkAgent 1
--renderer-client-id=672 1
--launch-time-ticks=76542560240 1
--renderer-client-id=676 1
--launch-time-ticks=76557205901 1

Note:

  1. The try, catch is needed as psutil cannot access some process details, as they are owned by the kernel.

Based on user feedback, adding some neat code, just to get the count for the process of interest.

import psutil
from collections import Counter

process_ctr = Counter()

def get_process_count(pname):
    for process in psutil.process_iter():
        try:
            process_cmd_list = process.cmdline()
            for p in process_cmd_list:
                process_ctr[p] += 1
        except Exception:
            continue

    for p, cnt in process_ctr.items():
        if pname in p:
            return cnt

if __name__ == '__main__':
    cnt = get_process_count('qterminal')
    print(f"The cnt is:{cnt}")
tenacity
  • 456
  • 1
  • 5
  • 14
  • Ok, thanks, that was more helpful than you can imagine. But more precisely, could you also tell me how I can make it so that my code returns how many instances of the terminal (aka /usr/bin/qterminal 3) are open, sorry if it's kind of stupid question, I'm a beginner in using Python. – Meaks Aug 10 '22 at 21:09
  • Hi @Meaks. I have updated the code based on your suggestion. Give it a shot, and let me know if it works. I tested it locally for my `bash` terminal. – tenacity Aug 10 '22 at 21:41
  • Also, if you are all set, please mark this answer as "Accepted". @Meaks – tenacity Aug 10 '22 at 21:58