PetitPlaid
PetitPlaid

Reputation: 91

How do I show processes for a specific uid or username using command line in python?

for opt, arg in opts:
   if opt in ('-u'):
     arg = numuid
     ps = subprocess.Popen('ps -u ', shell=True, stdout=subprocess.PIPE)
     print ps.stdout.read()

this is what i have so far, numuid is supposed to go after ps -u and is the uid. but what do i do to have it read in whatever numuid is?

Upvotes: 1

Views: 178

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180481

You just need to pass the name as a variable, if you just want to see or store the output check_output is the simplest method:

from subprocess import check_output

out = check_output(["ps","-u", numuid])

print(out)

For python 2.6:

from subprocess import PIPE,Popen

p = Popen(["ps","-u", numuid], stdout=PIPE)
out, err= p.communicate()
print(out)

You don't need shell=True when you pass a list of args.

Upvotes: 1

Related Questions