Search code examples
pythonpython-3.xpsutil

Python detect a python script's name in the list of processes


I know I can use psutil to get a list of running processes' names like this

import psutil

for i in psutil.pids():
    print(psutil.Process(i).name())

However, If i run a python script with python, psutil only will show me that I have an instance of python running.

So, my question is - if I run a python script:

python script_name

is it possible to detect script_name by psutil?


Solution

  • Look at psutil.Process(i).cmdline() docs. Your example would return

    ['python', 'script_name']