I want to get the user's name from a process ID in Python on Linux, so I can show it like this:
Name Username pid chrome wong2 123
You can read the UID (s) from / proc / pid / status . They are in a line that starts with Uid: . From the UID, you can get the username. UID = 1EUID = 2 DEF Master (PID): '' 'UIN Process PID' ('/ Proc /% d / status'% pid): If ln.startswith (' Uid : '): Uid = int (ln.split () [UID]) pwd.getpwuid (uid) .pw_name (Constants are derived from the Linux kernel.)
Comments
Post a Comment