您應該使用subprocess模塊蟒蛇。 如果你的日誌是不是太大了,你可以簡單地使用:
# for python >=2.7
result = subprocess.check_output(["/path/to/process_to_lauch", "arg1"])
# for python < 2.7
process = subprocess.Popen(["/path/to/process_to_lauch", "arg1"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
str_out, str_errr = process.communicate()
# in str_out you will find the standard output of your process
# in str_err you will find the standard output of your process
但如果你的輸出是真正的大(讓我們來談談在莫,而不是在KO),這可能會導致一些內存溢出... 在產量大的情況下,使用文件句柄輸出和錯誤:
out_file = open(out_file_name, "w")
err_file = open(out_file_name, "w")
process = subprocess.Popen(["/path/to/process_to_lauch", "arg1"], stdout=out_file, stderr=err_file)
return_code = process.wait()
out_file.close()
err_file.close()
,然後在out_file你會發現過程的輸出,並在err_file錯誤輸出。
當然,如果你想重新啓動它的過程時,把這個代碼放在一個循環;)