【发布时间】:2017-01-06 00:11:14
【问题描述】:
我有一个名为 generate_graphs.py 的 python 脚本,它使用 python 库生成图形。这些图表是我们通过内部数据向客户展示的趋势。
我正在尝试从 Java 运行脚本,但我没有看到任何证据表明它正在运行。没有日志显示它运行的证据,但我不确定这是脚本本身没有运行,还是它执行了 exec 方法。
脚本将数据插入数据库作为其进程的一部分,并且没有插入任何内容。但是,当单独从命令行运行脚本命令时,脚本运行得非常好。
这是 mkyong.com 使用的执行命令实现:
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
这个方法总共被调用了大约 40 次,大约每 3 秒调用一次:
/**
* Runs a command to execute the generate_graph python script
*
* @param server_id
*/
public void generateGraph(List<String> list_name, String server_id, String email_addr, String report_str) {
String generate_graph_cmd = "python2.7 generate_graphs.py --l '%s' --server_name '%s' --email_addr '%s' --report_string '%s' --debug";
//We want to remove the lm_ part from the server name
String server_name = server_id.split("_")[1].replace("\'", "");
String list_name_str = "";
for (String name : list_name){
list_name_str += name + ",";
}
//We want to remove the trailing comma left by the above loop
if (list_name_str.length() > 1){
list_name_str = list_name_str.substring(0, list_name_str.length() - 1);
}
generate_graph_cmd = String.format(generate_graph_cmd, list_name_str, server_name, email_addr, report_str);
try {
System.out.println("[Py Output] " + executeCommand(generate_graph_cmd));
} catch (Exception e) {
e.printStackTrace();
}
log.debug("Generating graph with the following parameters:\nserver_id: " + server_id + "\nlist_id: " + list_name.toString());
}
我只在日志中看到输出的log.debug 部分。我是否太快/不正确地调用它?任何帮助将不胜感激,谢谢!
【问题讨论】:
-
1. Java 代码约定使用camelCase(不是snake_case)。 2.更好的做法是使用
ProcessBuilder
标签: java python logging command-line command