[英]how to redirect shell script output to java console
I have a shell script which is sending output to console 我有一个将输出发送到控制台的shell脚本
ID=$PPID
echo the process id of the parent of this script is $ID
#fetch the parent of the program which executed this shell
read PID < <(exec ps -o ppid= "$ID")
echo the grand parent id of the script is $PID
read GPID < <(exec ps -o ppid= "$PID")
echo the grand parent id of the script is $GPID
while read -u 4 P; do
top -c -n 1 -p "$P"
done 4< <(exec pgrep -P "$PID") >&1
I am calling this script from java program and trying to display the output on console but only the output of echo
is appearing. 我正在从Java程序调用此脚本,并尝试在控制台上显示输出,但仅显示
echo
的输出。 The output of top command is not appearing on the java console. top命令的输出未出现在Java控制台上。
Here is my java program 这是我的java程序
import java.io.BufferedReader;
public class InformationFetcher {
public InformationFetcher() {
}
public static void main(String[] args) {
InformationFetcher informationFetcher = new InformationFetcher();
try {
Process process = Runtime.getRuntime().exec(
informationFetcher.getFilePath());
InputStream in = process.getInputStream();
printInputStream(in);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void printInputStream(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer outBuffer = new StringBuffer();
String newLine = System.getProperty("line.separator");
String line;
while ((line = reader.readLine()) != null) {
outBuffer.append(line);
outBuffer.append(newLine);
}
System.out.println(outBuffer.toString());
}
public String getFilePath() {
return this.getClass().getResource("/idFetcher.sh").getPath();
}
}
output is 输出是
the process id of the parent of this script is 3721
the grand parent id of the script is 3241
the grand parent id of the script is 3240
but it should also display output of top command. 但它也应该显示top命令的输出。 Any help would be greatly appreciated.
任何帮助将不胜感激。
I'm not quite sure about that how top
outputs, but it seems that top
doesn't send its result to STDOUT
in a normal way. 我不太确定
top
输出方式如何,但是top
似乎不会以正常方式将其结果发送到STDOUT
。
However, if you want top
to send its result to STDOUT
in a normal way, give it the -b
option: 但是,如果您希望
top
以正常方式将其结果发送到STDOUT
,请给它提供-b
选项:
top -b -n 1
# -b means in batch mode, which outputs to `STDOUT` normally
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.