1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import java.io.IOException;
21 import java.lang.management.ManagementFactory;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27
28
29
30
31 @InterfaceAudience.Private
32 public final class ProcessUtils {
33 private static Log LOG = LogFactory.getLog(ProcessUtils.class);
34
35 private ProcessUtils() { }
36
37 public static Integer getPid() {
38
39 String pidStr = System.getenv("JVM_PID");
40
41
42 if (pidStr == null || pidStr.trim().isEmpty()) {
43 String name = ManagementFactory.getRuntimeMXBean().getName();
44 if (name != null) {
45 int idx = name.indexOf("@");
46 if (idx != -1) {
47 pidStr = name.substring(0, name.indexOf("@"));
48 }
49 }
50 }
51 try {
52 if (pidStr != null) {
53 return Integer.valueOf(pidStr);
54 }
55 } catch (NumberFormatException nfe) {
56
57 }
58 return null;
59 }
60
61 public static Process runCmdAsync(List<String> cmd) {
62 try {
63 LOG.info("Running command async: " + cmd);
64 return new ProcessBuilder(cmd).inheritIO().start();
65 } catch (IOException ex) {
66 throw new IllegalStateException(ex);
67 }
68 }
69 }