-
Notifications
You must be signed in to change notification settings - Fork 138
/
ProcessUtils.java
208 lines (176 loc) · 6.24 KB
/
ProcessUtils.java
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package admin.util;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.util.*;
/**
* Lifted verbatim from GlassFish common-util
* @author Byron Nevins
*/
public final class ProcessUtils {
private ProcessUtils() {
// all static class -- no instances allowed!!
}
public static File getExe(String name) {
for (String path : paths) {
File f = new File(path + "/" + name);
if (f.canExecute()) {
return f.getAbsoluteFile();
}
}
return null;
}
/**
* Try and find the Process ID of "our" process.
* @return the process id or -1 if not known
*/
public static final int getPid() {
return pid;
}
/**
* Kill the process with the given Process ID.
* @param pid
* @return a String if the process was not killed for any reason including if it does not exist.
* Return null if it was killed.
*/
public static String kill(int pid) {
try {
String pidString = Integer.toString(pid);
ProcessManager pm = null;
String cmdline;
if (isWindows()) {
pm = new ProcessManager("taskkill", "/F", "/T", "/pid", pidString);
cmdline = "taskkill /F /T /pid " + pidString;
}
else {
pm = new ProcessManager("kill", "-9", "" + pidString);
cmdline = "kill -9 " + pidString;
}
pm.setEcho(false);
pm.execute();
int exitValue = pm.getExitValue();
if (exitValue == 0)
return null;
else
return "Error killing pid #" + pid;
}
catch (ProcessManagerException ex) {
return ex.getMessage();
}
}
/**
* If we can determine it -- find out if the process that owns the given
* process id is running.
* @param aPid
* @return true if it's running, false if not and null if we don't know.
* I.e the return value is a true tri-state Boolean.
*/
public static final Boolean isProcessRunning(int aPid) {
try {
if (isWindows())
return isProcessRunningWindows(aPid);
else
return isProcessRunningUnix(aPid);
}
catch (Exception e) {
return null;
}
}
//////////////////////////////////////////////////////////////////////////
////////// all private below /////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
private static final int pid;
private static final String[] paths;
private static boolean isProcessRunningWindows(int aPid) throws ProcessManagerException {
String pidString = Integer.toString(aPid);
ProcessManager pm = new ProcessManager("tasklist", "/FI", "\"pid eq " + pidString + "\"");
pm.setEcho(false);
pm.execute();
String out = pm.getStdout() + pm.getStderr();
/* output is either
(1)
INFO: No tasks running with the specified criteria.
(2)
Image Name PID Session Name Session# Mem Usage
========================= ====== ================ ======== ============
java.exe 3760 Console 0 64,192 K
*/
if (debug) {
System.out.println("------------ Output from tasklist ----------");
System.out.println(out);
System.out.println("------------------------------------------------");
}
if (ok(out)) {
if (out.indexOf("" + aPid) >= 0)
return true;
else
return false;
}
throw new ProcessManagerException("unknown");
}
private static Boolean isProcessRunningUnix(int aPid) throws ProcessManagerException {
ProcessManager pm = new ProcessManager("kill", "-0", "" + aPid);
pm.setEcho(false);
pm.execute();
int retval = pm.getExitValue();
return retval == 0 ? Boolean.TRUE : Boolean.FALSE;
}
static {
// variables named with 'temp' are here so that we can legally set the
// 2 final variables above.
int tempPid = -1;
try {
String pids = ManagementFactory.getRuntimeMXBean().getName();
int index = -1;
if (ok(pids) && (index = pids.indexOf('@')) >= 0) {
tempPid = Integer.parseInt(pids.substring(0, index));
}
}
catch (Exception e) {
tempPid = -1;
}
// final assignment
pid = tempPid;
String tempPaths = null;
if (isWindows()) {
tempPaths = System.getenv("Path");
if (!ok(tempPaths))
tempPaths = System.getenv("PATH"); // give it a try
}
else {
tempPaths = System.getenv("PATH");
}
if (ok(tempPaths))
paths = tempPaths.split(File.pathSeparator);
else
paths = new String[0];
}
private static boolean debug;
public static boolean isWindows() {
String osname = System.getProperty("os.name");
if(osname == null || osname.length() <= 0)
return false;
// case insensitive compare...
osname = osname.toLowerCase();
if(osname.indexOf("windows") >= 0)
return true;
return false;
}
static boolean ok(String s) {
return s != null && s.length() > 0;
}
}