我使用此方法call_shell()
连接Java和shell。
private static String call_shell(String command) throws IOException, InterruptedException{
Process proc = Runtime.getRuntime().exec(command);
if(proc.waitFor() != 0){
System.out.println("wrong in shell.");
}
String s = null;
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while((s = br.readLine()) != null){
sb.append(s).append("\r\n");
}
br.close();
return sb.toString();
}
但是,当我使用System.out.println(call_shell("/usr/local/bin/redis-server"));
调用此方法时,它将在控制台上被阻止,因为redis-server
将阻止bash。
所以我使用System.out.println(call_shell("/usr/local/bin/redis-server &"));
并想要运行它的背景。但是,结果是错误
wrong in shell.
24759:C 21 Apr 16:45:47.718 # Fatal error, can't open config file '&'
如何运行背景?