001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.util; 019 020import java.io.IOException; 021import java.lang.management.ManagementFactory; 022import java.util.List; 023import org.apache.yetus.audience.InterfaceAudience; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027/** 028 * Process related utilities. 029 */ 030@InterfaceAudience.Private 031public final class ProcessUtils { 032 private static Logger LOG = LoggerFactory.getLogger(ProcessUtils.class); 033 034 private ProcessUtils() { 035 } 036 037 public static Integer getPid() { 038 // JVM_PID is exported by bin/hbase run script 039 String pidStr = System.getenv("JVM_PID"); 040 041 // in case if it is not set correctly used fallback from mxbean which is implementation specific 042 if (pidStr == null || pidStr.trim().isEmpty()) { 043 String name = ManagementFactory.getRuntimeMXBean().getName(); 044 if (name != null) { 045 int idx = name.indexOf("@"); 046 if (idx != -1) { 047 pidStr = name.substring(0, name.indexOf("@")); 048 } 049 } 050 } 051 try { 052 if (pidStr != null) { 053 return Integer.valueOf(pidStr); 054 } 055 } catch (NumberFormatException nfe) { 056 // ignore 057 } 058 return null; 059 } 060 061 public static Process runCmdAsync(List<String> cmd) { 062 try { 063 LOG.info("Running command async: " + cmd); 064 return new ProcessBuilder(cmd).inheritIO().start(); 065 } catch (IOException ex) { 066 throw new IllegalStateException(ex); 067 } 068 } 069}