Java tutorial
/** * Copyright (c) 2012 Yahoo! Inc. All rights reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. See accompanying LICENSE file. */ package ape; import java.io.IOException; import java.io.InputStream; import org.apache.commons.cli.Option; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.ParseException; /** * This command will kill a node on the host. This node can be either a DataNode, * TaskTracker, NameNode, or JobTracker. * * This kill is executed in an identical fashion to running "kill -9 PID" on the * command line. * * */ public class KillNodeCommand extends ApeCommand { private Option option; /** * The constructor for this command simply creates its Option object (used by * the CLI parser) */ public KillNodeCommand() { option = OptionBuilder.withArgName("nodetype").hasArgs(1).withValueSeparator() .withDescription("Kills a datanode, tasktracker, jobtracker, or namenode.").withLongOpt("kill-node") .create("k"); } public String getName() { return option.getLongOpt(); } public Option getOption() { return option; } public boolean runImpl(String[] args) throws ParseException, IOException { if (Main.VERBOSE) { System.out.println("NodeType: " + args[0]); } String nodeType = args[0]; if (nodeType.toLowerCase().equals("datanode") || nodeType.toLowerCase().equals("tasktracker") || nodeType.toLowerCase().equals("jobtracker") || nodeType.toLowerCase().equals("namenode")) { System.out.println("Kill is about to execute the following command:"); /** * The line of code below sends a very ugly bash command to kill the corresponding process. * It gets a list of running processes, then greps it for the node type, then * removes the result generated by running grep, then it gets the process ID of that line * */ String cmd = "echo \"kill -9 \\$(ps -ef | grep -i " + nodeType + " | grep -v grep | grep -v ape | awk -F ' ' '{print \\$2}')\" > /tmp/kill-node.sh && chmod +x /tmp/kill-node.sh && /tmp/./kill-node.sh && rm /tmp/kill-node.sh"; System.out.println(cmd); ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd); pb.redirectErrorStream(true); Process sh = pb.start(); InputStream shIn = sh.getInputStream(); try { if (sh.waitFor() != 0) { System.out.println("Executing Kill Command failed"); return false; } } catch (InterruptedException e) { System.out.println( "The kill command was killed before it could kill its target process. Kind of ironic really..."); e.printStackTrace(); throw new RuntimeException(); } int c; while ((c = shIn.read()) != -1) { System.out.write(c); } try { shIn.close(); } catch (IOException e) { System.out.println("Could not close InputStream from the kill process."); e.printStackTrace(); return false; } } else { System.out.println("Invalid node type: " + nodeType); System.out.println("Should be one of the following: DataNode, TaskTracker, NameNode, JobTracker."); return false; } return true; } }