2015-04-12 21 views
-3

命令(不錯,renice只能)我正在做一個Python程序爲新值分配給進程和動態監視他們:電話UNIX在Python

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") 

我不知道該怎麼稱呼nicerenice與Python中的參數。

+0

我們不做* 「緊急」 *,這不是一個服務支持;見http://meta.stackexchange.com/q/6506/248731 – jonrsharpe

+0

@jonrsharpe:永遠不知道對不起。只是加急以獲得快速關注。 –

回答

1

使用subprocess

from subprocess import Popen,PIPE 

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

您也可以使用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) 
+0

OP導入的子過程,但從來沒有使用過heeeee – Hackaholic

+1

Paradic include'from subprocess import Popen' else else will importError :) – Hackaholic

+0

@Hackaholic,確實如此。乾杯。 –