-2

I'm making a Python program to assign new values to processes and dynamically monitor them:

import os
import subprocess
#!/usr/bin/env python
print "hello world"
os.system("ps lx")
print "Enter PID"
pid = raw_input()
print "Entered PID",pid
print "Enter new priority"
new = raw_input()
print "Entered new priority is"new
os.system("sudo renice new pid")

I don't know how to call nice and renice with arguments in Python.

jonrsharpe
  • 99,167
  • 19
  • 183
  • 334

1 Answers1

2

Use subprocess:

from subprocess import Popen,PIPE

p = Popen(["sudo","-S", "renice",new , pid], stdin=PIPE, stdout=PIPE, stderr=PIPE)
p.stdin.write("password\n")

You can use also subprocess.check_call:

from subprocess import Popen, PIPE,check_call

check_call(["ps", "lx"])
pid = raw_input("Enter PID")
print "Entered PID",pid
new = raw_input("Enter new priority")
print "Entered new priority is",new

p = Popen(["sudo","-S", "renice", new, pid], stdin=PIPE, stdout=PIPE, stderr=PIPE)
p.stdin.write("pc160780\n")
out, err = p.communicate()

print(out if out else err)
Padraic Cunningham
  • 160,756
  • 20
  • 201
  • 286