Splitting out the output of ps using Python

后端 未结 5 1060
轻奢々
轻奢々 2020-11-30 07:58

On Linux, the command ps aux outputs a list of processes with multiple columns for each stat. e.g.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START          


        
5条回答
  •  遥遥无期
    2020-11-30 08:22

    Check out the python.psutils package.

    psutil.process_iter returns a generator which you can use to iterate over all processes. p.cmdline is a list of each Process object's cmdline arguments, separated just the way you want.

    You can create a dictionary of pids vs (pid,cmdline,path) with just one line and then use it anyway you want.

    pid_dict = dict([(p.pid, dict([('pid',p.pid), ('cmdline',p.cmdline), ('path',p.path)]))
                     for p in psutil.process_iter()]))
    

提交回复
热议问题