【发布时间】:2018-01-17 05:08:20
【问题描述】:
问题:
当通过Runtime.exec(...) 执行以下命令时,它在寻找匹配的引号字符时失败并出现意外的 EOF。
一个奇怪的地方是错误消息有一个严重的字符,后跟两个单引号。
但是,当我执行通过 putty 打印在日志中的命令时,它工作正常。
命令:
bin/sh -c 'ps -eo uname,pid,ppid,nlwp,pcpu,pmem,psr,start_time,tty,time,args | fgrep IAAPC | fgrep /f1/f2/a00-a/f3/server/server_1/env_1/javadriver | fgrep -v fgrep'
产生的错误:
-eo: -c: line 0: unexpected EOF while looking for matching `''
-eo: -c: line 1: syntax error: unexpected end of file
Java 代码(Java 1.6 ...不要评判):
String driverHome = trimToEmpty(System.getProperty("batchdriver.home"));
String cmd = "/bin/sh -c 'ps -eo uname,pid,ppid,nlwp,pcpu,pmem,psr,start_time,tty,time,args | fgrep "+jobName+" | fgrep "+driverHome+" | fgrep -v fgrep'";
String out = null, err = null;
Process proc = null;
try {
proc = Runtime.getRuntime().exec(cmd);
out = fullyRead(proc.getInputStream());
err = fullyRead(proc.getErrorStream());
int exitVal = proc.waitFor();
if(logger.isDebugEnabled()) {
logger.debug("Process Information: "+out);
}
if (isNotEmpty(err)) {
logger.error(failedCommandMessage(cmd, out, err));
this.processId = null;
this.processDesc = PROCESS_NOT_FOUND;
return;
}
String[] processes = StringUtils.split(out, "\r?\n");
if (processes == null || processes.length == 0) {
this.processDesc = PROCESS_NOT_FOUND;
}
else if (processes.length == 1) {
String[] processInfo = processes[0].split("\\s+");
this.processId = processInfo[1];
if (!isNumeric(this.processId)) {
this.processId = null;
}
this.processDesc = out;
}
else {
this.processDesc = out;
}
if (logger.isDebugEnabled()) {
logger.debug("Call to the OS completed with exit value: " + exitVal);
}
} catch (Exception e) {
try {out = fullyRead(proc.getInputStream());} catch (Exception e1) {}
try {err = fullyRead(proc.getErrorStream());} catch (Exception e1) {}
this.processId = null;
this.processDesc = PROCESS_NOT_FOUND;
logger.error(failedCommandMessage(cmd, out, err), e);
}
【问题讨论】:
-
反引号、单引号、单引号只是这个工具输出它想要“引用”的项目的方式。不幸的是,许多其他工具也使用这种格式,但我想它可以避免在丢失的字符是
"(可能还有其他)的情况下产生歧义。对于您的情况,您可以认为错误消息只是以"'"(dbl-quote,单引号,dbl-quote)结尾,而'是有问题的字符。为什么会发生这种情况超出了我的工资等级。祝你好运!
标签: java unix runtime.exec java-6