Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 880
GriMel Avatar asked May 05 '26 03:05

GriMel


1 Answers

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

['python', 'script_name']
like image 59
sberry Avatar answered May 07 '26 16:05

sberry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!