简体   繁体   中英

Process command using python

I want to pick up the command and its arguments in Python.

I can use

process=os.popen('ps -elf').read().split("\n")

and then use regular expression to extract the command but its ugly.

psutils returns a process name but not the actual commands and arguments

Is there a simple way to do this?

psutil can get the command line arguments:

import psutil

for p in psutil.process_iter():
    cmd_line = p.cmdline()
    if cmd_line:
        print(cmd_line)

EDIT: updated to fix the issue found by @Keir

The last suggestion is almost correct. It should be

for p in psutil.process_iter():
   cline = p.cmdline
   if cline: print(cline)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM