Linux自动清理文件保留一个月功能实现

main{
List commandArr = new ArrayList<>();
commandArr.add("/bin/sh");
commandArr.add("-c");
commandArr.add(“find /data/ -name ‘20*’ -type d -ctime +30 | xargs rm -rf”);
String result = run(commandArr.toArray(new String[commandArr.size()]));
String result = run(commandArr.toArray(new String[commandArr.size()]));
public static String run(String[] command) throws IOException {
Scanner input = null;
String result = “”;
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
try {
//等待命令执行完成
process.waitFor(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
InputStream is = process.getInputStream();
input = new Scanner(is);
while (input.hasNextLine()) {
result += input.nextLine() + “\n”;
}
result = command + “\n” + result; //加上命令本身,打印出来
} finally {
if (input != null) {
input.close();
}
if (process != null) {
process.destroy();
}
}
return result;
}
}

你可能感兴趣的:(维护,linux,java)