Splitting out the output of ps using Python

后端 未结 5 1108
轻奢々
轻奢々 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:30

    Here's a nice routine and usage to get you going:

    def getProcessData():
        ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
        processes = ps.split('\n')
        # this specifies the number of splits, so the splitted lines
        # will have (nfields+1) elements
        nfields = len(processes[0].split()) - 1
        retval = []
        for row in processes[1:]:
            retval.append(row.split(None, nfields))
        return retval
    
    wantpid = int(contents[0])
    pstats = getProcessData()
    for ps in pstats:
        if (not len(ps) >= 1): continue
        if (int(ps[1]) == wantpid):
            print "process data:"
            print "USER              PID       %CPU        %MEM       VSZ        RSS        TTY       STAT      START TIME      COMMAND"
            print "%-10.10s %10.10s %10.10s %10.10s %10.10s %10.10s %10.10s %10.10s %10.10s  %s" % (ps[0], ps[1], ps[2], ps[3], ps[4], ps[5], ps[6], ps[7], ps[8], ps[9])
    

提交回复
热议问题