public NodeHealthMonitorExecutor(String[] args) { ArrayList<String> execScript = new ArrayList<String>(); execScript.add(nodeHealthScript); if (args != null) { execScript.addAll(Arrays.asList(args)); } shexec = new ShellCommandExecutor(execScript .toArray(new String[execScript.size()]), null, null, scriptTimeout); }
/** * Initialize. * * @param configuration */ public void init(String location, long timeout) { this.healthCheckScript = location; this.scriptTimeout = timeout; ArrayList<String> execScript = new ArrayList<>(); execScript.add(healthCheckScript); this.shexec = new ShellCommandExecutor(execScript.toArray(new String[execScript.size()]), null, null, scriptTimeout); LOG.info("HealthChecker initialized with script at " + this.healthCheckScript + ", timeout=" + timeout); }
dir = new File(userDir); ShellCommandExecutor s = new ShellCommandExecutor( cmdList.toArray(new String[cmdList.size()]), dir); try {
public void testShellCommandExecutorToString() throws Throwable { Shell.ShellCommandExecutor sce=new Shell.ShellCommandExecutor( new String[] { "ls", "..","arg 2"}); String command = sce.toString(); assertInString(command,"ls"); assertInString(command, " .. "); assertInString(command, "\"arg 2\""); }
private static void sendSignal(String pid, int signal) throws IOException { ShellCommandExecutor shexec = null; String[] arg = { "kill", "-" + signal, "--", pid }; shexec = new ShellCommandExecutor(arg); shexec.execute(); }
private static void sendSignal(String pid, int signal) throws IOException { ShellCommandExecutor shexec = null; String[] arg = { "kill", "-" + signal, "--", pid }; shexec = new ShellCommandExecutor(arg); shexec.execute(); }
/** * Create a ShellCommandExecutor object for fetch a user's group id list. * * @param userName the user's name * @return a ShellCommandExecutor object */ protected ShellCommandExecutor createGroupIDExecutor(String userName) { return new ShellCommandExecutor( Shell.getGroupsIDForUserCommand(userName), null, null, 0L); }
String getSystemInfoInfoFromShell() { try { ShellCommandExecutor shellExecutor = new ShellCommandExecutor( new String[] {Shell.getWinUtilsFile().getCanonicalPath(), "systeminfo" }); shellExecutor.execute(); return shellExecutor.getOutput(); } catch (IOException e) { LOG.error(StringUtils.stringifyException(e)); } return null; }
String getAllProcessInfoFromShell() { try { ShellCommandExecutor shellExecutor = new ShellCommandExecutor( new String[] {Shell.getWinUtilsFile().getCanonicalPath(), "task", "processList", taskProcessId }); shellExecutor.execute(); return shellExecutor.getOutput(); } catch (IOException e) { LOG.error(StringUtils.stringifyException(e)); } return null; }
shExec = new ShellCommandExecutor(cmd, linkFile.getParentFile()); } else { shExec = new ShellCommandExecutor(cmd);
BufferedReader in = null; ShellCommandExecutor shexec = new ShellCommandExecutor(cmd); try { shexec.execute();
try { String[] args = {"setsid", "bash", "-c", "echo $$"}; shexec = new ShellCommandExecutor(args); shexec.execute(); } catch (IOException ioe) {
private static void unTarUsingTar(File inFile, File untarDir, boolean gzipped) throws IOException { StringBuffer untarCommand = new StringBuffer(); if (gzipped) { untarCommand.append(" gzip -dc '"); untarCommand.append(FileUtil.makeSecureShellPath(inFile)); untarCommand.append("' | ("); } untarCommand.append("cd '"); untarCommand.append(FileUtil.makeSecureShellPath(untarDir)); untarCommand.append("' && "); untarCommand.append("tar -xf "); if (gzipped) { untarCommand.append(" -)"); } else { untarCommand.append(FileUtil.makeSecureShellPath(inFile)); } String[] shellCmd = { "bash", "-c", untarCommand.toString() }; ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd); shexec.execute(); int exitcode = shexec.getExitCode(); if (exitcode != 0) { throw new IOException("Error untarring file " + inFile + ". Tar process exited with exit code " + exitcode); } }
/** * Change the permissions on a file / directory, recursively, if * needed. * @param filename name of the file whose permissions are to change * @param perm permission string * @param recursive true, if permissions should be changed recursively * @return the exit code from the command. * @throws IOException */ public static int chmod(String filename, String perm, boolean recursive) throws IOException { String [] cmd = Shell.getSetPermissionCommand(perm, recursive); String[] args = new String[cmd.length + 1]; System.arraycopy(cmd, 0, args, 0, cmd.length); args[cmd.length] = new File(filename).getPath(); ShellCommandExecutor shExec = new ShellCommandExecutor(args); try { shExec.execute(); }catch(IOException e) { if(LOG.isDebugEnabled()) { LOG.debug("Error while changing permission : " + filename +" Exception: " + StringUtils.stringifyException(e)); } } return shExec.getExitCode(); }
public static boolean checkIsBashSupported() throws InterruptedIOException { if (Shell.WINDOWS) { return false; } ShellCommandExecutor shexec; boolean supported = true; try { String[] args = {"bash", "-c", "echo 1000"}; shexec = new ShellCommandExecutor(args); shexec.execute(); } catch (InterruptedIOException iioe) { LOG.warn("Interrupted, unable to determine if bash is supported", iioe); throw iioe; } catch (IOException ioe) { LOG.warn("Bash is not supported by the OS", ioe); supported = false; } catch (SecurityException se) { LOG.info("Bash execution is not allowed by the JVM " + "security manager.Considering it not supported."); supported = false; } return supported; }
/** * Static method to execute a shell command. * Covers most of the simple cases without requiring the user to implement * the <code>Shell</code> interface. * @param env the map of environment key=value * @param cmd shell command to execute. * @param timeout time in milliseconds after which script should be marked timeout * @return the output of the executed command. * @throws IOException on any problem. */ public static String execCommand(Map<String, String> env, String[] cmd, long timeout) throws IOException { ShellCommandExecutor exec = new ShellCommandExecutor(cmd, null, env, timeout); exec.execute(); return exec.getOutput(); }
private static long getConf(String attr) { if(Shell.LINUX) { try { ShellCommandExecutor shellExecutorClk = new ShellCommandExecutor( new String[] {"getconf", attr }); shellExecutorClk.execute(); return Long.parseLong(shellExecutorClk.getOutput().replace("\n", "")); } catch (IOException|NumberFormatException e) { return -1; } } return -1; }
/** * Create a ShellCommandExecutor object for fetch a user's group id list. * * @param userName the user's name * @return a ShellCommandExecutor object */ protected ShellCommandExecutor createGroupIDExecutor(String userName) { return new ShellCommandExecutor( getGroupsIDForUserCommand(userName), null, null, timeout); }
/** * Create a ShellCommandExecutor object using the user's name. * * @param userName user's name * @return a ShellCommandExecutor object */ protected ShellCommandExecutor createGroupExecutor(String userName) { return new ShellCommandExecutor( getGroupsForUserCommand(userName), null, null, timeout); }
String getSystemInfoInfoFromShell() { try { ShellCommandExecutor shellExecutor = new ShellCommandExecutor( new String[] {Shell.getWinUtilsFile().getCanonicalPath(), "systeminfo" }); shellExecutor.execute(); return shellExecutor.getOutput(); } catch (IOException e) { LOG.error(StringUtils.stringifyException(e)); } return null; }