Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python进程包装器_Python - Fatal编程技术网

python进程包装器

python进程包装器,python,Python,我有一个java程序,它在ubuntu的特定端口上运行。在运行程序时,我需要从程序中获取输出并将其保存在日志文件中。我当前使用nohub运行它们。当它们失败时,我不知道它们失败的原因。然后进程重新启动nohub,使其被覆盖。我希望进程重新启动并更新日志文件,我可以在以后检查它。目前我不知道它的状态,是在运行还是失败 我听说使用python脚本很容易做到这一点。 谁能帮我做这个 提前谢谢 Renjith Raj您应该使用python模块。 如果您的日志不太大,您可以简单地使用: # for pyt

我有一个java程序,它在ubuntu的特定端口上运行。在运行程序时,我需要从程序中获取输出并将其保存在日志文件中。我当前使用nohub运行它们。当它们失败时,我不知道它们失败的原因。然后进程重新启动nohub,使其被覆盖。我希望进程重新启动并更新日志文件,我可以在以后检查它。目前我不知道它的状态,是在运行还是失败

我听说使用python脚本很容易做到这一点。 谁能帮我做这个

提前谢谢 Renjith Raj

您应该使用python模块。 如果您的日志不太大,您可以简单地使用:

# 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
然后,在out_文件中,您将找到进程的输出,在err_文件中,您将找到错误输出

当然,如果您想在进程死时重新启动它,请将此代码放入循环中;)

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