Finding the command for a specific PID in Linux from Python

后端 未结 7 903
半阙折子戏
半阙折子戏 2020-12-19 05:33

I\'d like to know if it\'s possible to find out the \"command\" that a PID is set to. When I say command, I mean what you see in the last column when you run the command \"t

7条回答
  •  感动是毒
    2020-12-19 06:20

    This worked for me:

    def filter_non_printable(str):
        ret=""
        for c in str:
            if ord(c) > 31 or ord(c) == 9:
                ret += c
            else:
                ret += " "
        return ret
    
    
    #
    # Get /proc//cmdline information
    #
    def pid_name(pid):
        try:
            with open(os.path.join('/proc/', pid, 'cmdline'), 'r') as pidfile:
                return filter_non_printable(pidfile.readline())
    
        except Exception:
            pass
            return
    

提交回复
热议问题