Tôi có một kịch bản python 'b.py' in ra thời gian bao giờ 5 giây.Làm thế nào tôi có thể gọi một kịch bản python từ một tập lệnh python
while (1):
print "Start : %s" % time.ctime()
time.sleep(5)
print "End : %s" % time.ctime()
time.sleep(5)
Và trong a.py của tôi, tôi gọi b.py bởi:
def run_b():
print "Calling run b"
try:
cmd = ["./b.py"]
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in iter(p.stdout.readline, b''):
print (">>>" + line.rstrip())
except OSError as e:
print >>sys.stderr, "fcs Execution failed:", e
return None
và sau này, tôi giết 'b.py' bởi: PS_PATH = "/ usr/bin/ps -efW "
def kill_b(program):
try:
cmd = shlex.split(PS_PATH)
retval = subprocess.check_output(cmd).rstrip()
for line in retval.splitlines():
if program in line:
print "line =" + line
pid = line.split(None)[1]
os.kill(int(pid), signal.SIGKILL)
except OSError as e:
print >>sys.stderr, "kill_all Execution failed:", e
except subprocess.CalledProcessError as e:
print >>sys.stderr, "kill_all Execution failed:", e
run_b()
time.sleep(600)
kill_b("b.py")
Tôi có 2 câu hỏi. 1. tại sao tôi không thấy bất kỳ bản in nào từ 'b.py' và khi tôi làm 'ps -efW' Tôi không thấy quy trình có tên 'b.py'? 2. Tại sao khi tôi giết một quá trình như trên, tôi thấy 'quyền bị từ chối'?
Tôi đang chạy trên tập lệnh trên Cygwin bên dưới cửa sổ.
Cảm ơn bạn.
python khởi động – Nick
cách dễ dàng hơn để xóa 'b.py' là trả về' p' từ 'run_b()' trước khi bắt đầu đọc đầu ra của 'b.py' và gọi' p.kill(); p.wait() 'sau. – jfs
kiểm tra xem './B.py' có được khởi động thành công không (' p.poll() 'nên là 'None') – jfs