[英]How to invoke a unix script from java?
I want to delete the old log files in log directory. 我想删除日志目录中的旧日志文件。 To delete the log files which are more than 6 months, I have written the script like
要删除超过6个月的日志文件,我编写了如下脚本
find /dnbusr1/ghmil/BDELogs/import -type f -mtime +120 -exec rm -f {} \\; 查找/ dnbusr1 / ghmil / BDELogs / import -type f -mtime +120 -exec rm -f {} \\;
By using this script i am able to delete the old files, but how do I invoke this script by using java? 通过使用此脚本,我可以删除旧文件,但是如何使用Java调用此脚本?
If portability is an issue, preventing you from running with Runtime.exec(), this code is quite trivial to write in Java using File and a FilenameFilter. 如果可移植性是一个问题,使您无法使用Runtime.exec()运行,那么使用Java使用File和FilenameFilter编写此代码非常简单。
Edit: Here is a static method to delete a directory tree... you can add in the filtering logic easily enough: 编辑:这是删除目录树的静态方法...您可以足够容易地在过滤逻辑中添加:
static public int deleteDirectory(File dir, boolean slf) {
File[] dc; // directory contents
String dp; // directory path
int oc=0; // object count
if(dir.exists()) {
dir=dir.getAbsoluteFile();
if(!dir.canWrite()) {
throw new IoEscape(IoEscape.NOTAUT,"Not authorized to delete directory '"+dir+"'.");
}
dp=dir.getPath();
if(dp.equals("/") || dp.equals("\\") || (dp.length()==3 && dp.charAt(1)==':' && (dp.charAt(2)=='/' || dp.charAt(2)=='\\'))) {
// Prevent deletion of the root directory just as a basic restriction
throw new IoEscape(IoEscape.GENERAL,"Cannot delete root directory '"+dp+"' using IoUtil.deleteDirectory().");
}
if((dc=dir.listFiles())!=null) {
for(int xa=0; xa<dc.length; xa++) {
if(dc[xa].isDirectory()) {
oc+=deleteDirectory(dc[xa]);
if(!dc[xa].delete()) { throw new IoEscape(IoEscape.GENERAL,"Unable to delete directory '"+dc[xa]+"' - it may not be empty, may be in use as a current directory, or may have restricted access."); }
}
else {
if(!dc[xa].delete()) { throw new IoEscape(IoEscape.GENERAL,"Unable to delete file '"+dc[xa]+"' - it may be currently in use by a program, or have restricted access."); }
}
oc++;
}
}
if(slf) {
if(!dir.delete()) { throw new IoEscape(IoEscape.GENERAL,"Unable to delete directory '"+dir+"' - it may not be empty, may be in use as a current directory, or may have restricted access."); }
oc++;
}
}
return oc;
}
When you only want to call the command you described call: 当您只想调用所描述的命令时,请调用:
Runtime r = Runtime.getRuntime();
Process process = r.exec("find /dnbusr1/ghmil/BDELogs/import -type f -mtime +120 -exec rm -f {} \\;"); //$NON-NLS-1$
process.waitFor();
If you want to call more than one command use Chris Jester-Young answer. 如果要调用多个命令,请使用Chris Jester-Young答案。 The exec method can also define files you want to use.
exec方法还可以定义您要使用的文件。 The other answers link the method describtion.
其他答案链接方法描述。
Using system calls in Java is possible, but is generally a bad idea because you'll lose the portability of the code. 在Java中使用系统调用是可能的,但是通常是个坏主意,因为您将失去代码的可移植性。 You could look into Ant and use something like this purge task .
您可以调查Ant并使用类似此清除任务的工具 。
Adding to Crashworks's answer, you call with these arguments in the cmdarray
: 添加到Crashworks的答案中,您可以在
cmdarray
使用以下参数进行cmdarray
:
new String[] {"find", "/dnbusr1/ghmil/BDELogs/import", "-type", "f",
"-mtime", "+120", "-exec", "rm", "-f", "{}", ";"}
If your find
supports the -exec ... {} +
syntax, change the ";"
如果
find
支持-exec ... {} +
语法,请更改";"
at the end to "+"
. 最后是
"+"
。 It will make your command run faster (it will call rm
on a batch of files at once, rather than once for each file). 这将使您的命令运行得更快(它将对一批文件一次调用
rm
,而不是对每个文件一次调用rm
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.