import psutil
for p in psutil.process_iter():
if p.name() == 'foo':
print p.cmdline()
print ' '.join(p.cmdline())
PROCESS:
foo -p "Hello World"
RESULT:
['foo', '-p', 'Hello World']
foo -p Hello World
I wish to get the original command line for a process as in foo -p "Hello World"
, I've tried to use cmdline()
but it provides parameters tokenized, which I'm not interested into, and joining them doesn't produce the expected result all the time, as can be seen from result's second print.
Is there a way to achieve this preferably in a portable way (maybe using other library or doing it differently)?
EDIT
This is for sake of comparison with an already stored command. Another solution to my problem could be that just ' '.join
would be ok if I could get the static command string (foo -p "Hello World"
) without the quotes that are around args to do the comparison. Meaning, if there were a function that did the shell param tokenization that psutil is doing, it would solve my command comparison problem.