ProcessUtil.java
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package io.mycat.util;
import io.mycat.migrate.MigrateUtils;
import io.mycat.util.dataMigrator.DataMigratorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Arrays;
import java.util.List;
public class ProcessUtil
{
private static Logger LOGGER = LoggerFactory.getLogger((ProcessUtil.class));
public static int exec(String cmd) {
Process process = null;
try {
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(cmd);
new StreamGobble(process.getInputStream(), "INFO").start();
new StreamGobble(process.getErrorStream(), "ERROR").start();
return process.waitFor();
} catch (Throwable t) {
LOGGER.error(t.getMessage());
} finally {
if (process != null)
process.destroy();
}
return 0;
}
public static String execReturnString(List<String> cmd) {
Process process = null;
try {
// Runtime runtime = Runtime.getRuntime();
// process = runtime.exec(cmd);
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
process=pb.start();
StreamGobble inputGobble = new StreamGobble(process.getInputStream(), "INFO");
inputGobble.start();
new StreamGobble(process.getErrorStream(), "ERROR").start();
process.waitFor();
return inputGobble.getResult();
} catch (Throwable t) {
LOGGER.error(t.getMessage());
} finally {
if (process != null)
process.destroy();
}
return null;
}
public static String execReturnString(String cmd) {
Process process = null;
try {
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(cmd);
StreamGobble inputGobble = new StreamGobble(process.getInputStream(), "INFO");
inputGobble.start();
new StreamGobble(process.getErrorStream(), "ERROR").start();
process.waitFor();
return inputGobble.getResult();
} catch (Throwable t) {
LOGGER.error(t.getMessage());
} finally {
if (process != null)
process.destroy();
}
return null;
}
public static int exec(String cmd,File dir) {
Process process = null;
try {
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(cmd,null,dir);
new StreamGobble(process.getInputStream(), "INFO").start();
new StreamGobble(process.getErrorStream(), "ERROR").start();
return process.waitFor();
} catch (Throwable t) {
LOGGER.error(t.getMessage());
} finally {
if (process != null)
process.destroy();
}
return 0;
}
public static void main(String[] args) {
// List<String> argss= Arrays.asList("mysqldump", "-h127.0.0.1", "-P3301", "-uczn",
// "-p123", "base1","test", "--single-transaction","-q","--default-character-set=utf8mb4","--hex-blob","--where=(_slot>=100 and _slot<=1000) or (_slot>=2000 and _slot <=100000)", "--master-data=1","-Tc:\\999"
// ,"--fields-enclosed-by=\\\"","--fields-terminated-by=,", "--lines-terminated-by=\\n", "--fields-escaped-by=\\\\");
// String result= ProcessUtil.execReturnString(argss);
// System.out.println(result);
}
}