I would like to check if a script is running with a specific command line argument within a python script.
For example I would like to check if:
main.py testarg
Is running. Is there any way I can achieve this?
Thanks in advance
I would like to check if a script is running with a specific command line argument within a python script.
For example I would like to check if:
main.py testarg
Is running. Is there any way I can achieve this?
Thanks in advance
Here is my take of it:
import psutil
L=[(p.name(),p.cmdline()[1]) for p in psutil.process_iter() if p.name().startswith('py')]
Now if you print this list L out:
('py.exe', 'programs\\pad29.py')
('python.exe', 'programs\\pad29.py')
('python.exe', '-u')
....
('python.exe', 'programs\\test_signal.py')
The list contains the command line parameters of every running process whose name starts with 'py'. That would include python.exe and py.exe. The condition is necessary otherwise you may run into some AccessDenied error if you try to poke on every running Windows process, like some smss.exe. Once you have this list, finding out what you want is a no brainer:
t=['what_you_want' == m[1] for m in L]
answer=True in t
This helped me when I wanted to match specific process with specific arguments :
import psutil
for proc in psutil.process_iter():
if ( proc.name().startswith('ruby')) and ("service-buffer.rb" in proc.cmdline()[1]) :
print("match", proc.cmdline()[1])
import psutil
for p in psutil.process_iter():
try: # else get PermissionError
pid = p.pid
cmdline = p.cmdline()
print(pid)
for cmd in cmdline:
print(cmd)
print()
except:
pass