日期:2014-05-16  浏览次数:20622 次

Java 执行linux 中的脚本
private static Logger logger = Logger.getLogger(CmdAccess.class);	
	public static Boolean IsPrimary()
	{
		logger.info("IsPrimary : BEGIN");
		String value = "";
		Boolean retValue = false;
		
		value = ExecuteCmd("/bin/sh /opt/client/bin/IsPrimary.sh");
		logger.info("IsPrimary : return value of bootconfig.sh -myprisec : " + value);
		if (value.equalsIgnoreCase("Pri"))
		{
			retValue = true;
		}
		logger.info("IsPrimary : END");
		return retValue;
	}
	
	public static String ExecuteCmd(String cmd) { 
		
		logger.info("ExecuteCmd : BEGIN");
		String out = ""; 
		try 
		{ 
  			logger.info("ExecuteCmd : before runtime exec "+  cmd);
	
			Process p_pl = Runtime.getRuntime().exec( cmd );

			BufferedReader br_pl = new BufferedReader( new InputStreamReader( p_pl.getInputStream() ) );
			logger.info("ExecuteCmd : after getInputStream "+  cmd);
			try
			{
				out = br_pl.readLine();
    				logger.info("ExecuteCmd : output - " + out);
			}
			catch (IOException e) 
			{ 
				logger.error("ExecuteCmd : Exception while executing bootconfig script1 - " + e.getMessage());
			} 
		} 
		catch (IOException e1) 
		{ 
			logger.error("ExecuteCmd : Exception while executing bootconfig script - " + e1.getMessage()); 
		}
		logger.info("ExecuteCmd : END");
		return out;
		
	}