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; 023 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027import org.apache.yetus.audience.InterfaceAudience; 028 029/** 030 * Process related utilities. 031 */ 032@InterfaceAudience.Private 033public final class ProcessUtils { 034 private static Logger LOG = LoggerFactory.getLogger(ProcessUtils.class); 035 036 private ProcessUtils() { } 037 038 public static Integer getPid() { 039 // JVM_PID is exported by bin/hbase run script 040 String pidStr = System.getenv("JVM_PID"); 041 042 // in case if it is not set correctly used fallback from mxbean which is implementation specific 043 if (pidStr == null || pidStr.trim().isEmpty()) { 044 String name = ManagementFactory.getRuntimeMXBean().getName(); 045 if (name != null) { 046 int idx = name.indexOf("@"); 047 if (idx != -1) { 048 pidStr = name.substring(0, name.indexOf("@")); 049 } 050 } 051 } 052 try { 053 if (pidStr != null) { 054 return Integer.valueOf(pidStr); 055 } 056 } catch (NumberFormatException nfe) { 057 // ignore 058 } 059 return null; 060 } 061 062 public static Process runCmdAsync(List<String> cmd) { 063 try { 064 LOG.info("Running command async: " + cmd); 065 return new ProcessBuilder(cmd).inheritIO().start(); 066 } catch (IOException ex) { 067 throw new IllegalStateException(ex); 068 } 069 } 070}