Example usage for org.apache.commons.exec DefaultExecuteResultHandler DefaultExecuteResultHandler

List of usage examples for org.apache.commons.exec DefaultExecuteResultHandler DefaultExecuteResultHandler

Introduction

In this page you can find the example usage for org.apache.commons.exec DefaultExecuteResultHandler DefaultExecuteResultHandler.

Prototype

public DefaultExecuteResultHandler() 

Source Link

Document

Constructor.

Usage

From source file:beans.ServerBootstrapperImpl.java

private void createNewMachine(ServerNode serverNode) {
    File cloudFolder = null;/*  w ww  .  ja v a 2 s. co  m*/
    ComputeServiceContext jCloudsContext = null;
    try {
        // no existing management machine - create new server
        String project = serverNode.getProject();
        String secretKey = serverNode.getSecretKey();
        String apiKey = serverNode.getKey();
        logger.info("Creating cloud folder with specific user credentials. Project: [{}], api key: [{}]",
                project, apiKey);
        jCloudsContext = CloudifyUtils.createJcloudsContext(project, apiKey, secretKey);
        cloudFolder = CloudifyUtils.createCloudFolder(project, apiKey, secretKey, jCloudsContext);
        logger.info("cloud folder is at [{}]", cloudFolder);

        logger.info("Creating security group for user.");
        CloudifyUtils.createCloudifySecurityGroup(jCloudsContext);

        //Command line for bootstrapping remote cloud.
        CommandLine cmdLine = new CommandLine(conf.server.cloudBootstrap.remoteBootstrap.getAbsoluteFile());
        cmdLine.addArgument(cloudFolder.getName());

        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        ProcExecutor bootstrapExecutor = executorFactory.getBootstrapExecutor(serverNode);

        logger.info("Executing command line: " + cmdLine);
        bootstrapExecutor.execute(cmdLine, ApplicationContext.get().conf().server.environment.getEnvironment(),
                resultHandler);
        logger.info("waiting for output");
        resultHandler.waitFor();
        logger.info("finished waiting , exit value is [{}]", resultHandler.getExitValue());

        String output = Utils.getOrDefault(Utils.getCachedOutput(serverNode), "");
        if (resultHandler.getException() != null) {
            logger.info("we have exceptions, checking for known issues");
            if (output.contains("found existing management machines")) {
                logger.info(
                        "found 'found existing management machines' - issuing cloudify already exists message");
                throw new ServerException(Messages.get("cloudify.already.exists"));
            }
            logger.info("Command execution ended with errors: {}", output);
            throw new RuntimeException("Failed to bootstrap cloudify machine: " + output,
                    resultHandler.getException());
        }

        logger.info("finished handling errors, extracting IP");
        String publicIp = Utils.extractIpFromBootstrapOutput(output);
        if (StringUtils.isEmpty(publicIp)) {
            logger.warn("No public ip address found in bootstrap output. " + output);
            throw new RuntimeException("Bootstrap failed. No IP address found in bootstrap output." + output,
                    resultHandler.getException());
        }
        logger.info("ip is [{}], saving to serverNode", publicIp);

        String privateKey = CloudifyUtils.getCloudPrivateKey(cloudFolder);
        if (StringUtils.isEmpty(privateKey)) {
            throw new RuntimeException("Bootstrap failed. No pem file found in cloud directory.");
        }
        logger.info("found PEM string");
        logger.info("Bootstrap cloud command ended successfully");

        logger.info("updating server node with new info");
        serverNode.setPublicIP(publicIp);
        serverNode.setPrivateKey(privateKey);

        serverNode.save();
        logger.info("server node updated and saved");
    } catch (Exception e) {
        serverNode.errorEvent("Invalid Credentials").save();
        throw new RuntimeException("Unable to bootstrap cloud", e);
    } finally {
        if (cloudFolder != null && conf.server.cloudBootstrap.removeCloudFolder) {
            FileUtils.deleteQuietly(cloudFolder);
        }
        if (jCloudsContext != null) {
            jCloudsContext.close();
        }
        serverNode.setStopped(true);

    }
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * Executes a PBS command./*from  w  w  w.ja  va 2s  . c  o  m*/
 *
 * @param cmdLine command
 * @param environment env vars
 * @param out output stream
 * @param err err stream
 * @return execute handler
 * @throws ExecuteException if there is an error executing a command
 * @throws IOException in case of an IO problem
 */
static DefaultExecuteResultHandler execute(CommandLine cmdLine, Map<String, String> environment,
        OutputStream out, OutputStream err) throws ExecuteException, IOException {
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ExecuteStreamHandler streamHandler = new PumpStreamHandler(out, err);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.setStreamHandler(streamHandler);
    if (environment != null) {
        executor.execute(cmdLine, environment, resultHandler);
    } else {
        executor.execute(cmdLine, resultHandler);
    }
    return resultHandler;
}

From source file:com.tibco.tgdb.test.lib.TGServer.java

/**
 * Start the TG server synchronously./*from ww w. j ava 2  s .com*/
 * 
 * @param timeout
 *            Number of milliseconds allowed to start the server
 * @throws TGStartException Start operation fails
 */
public void start(long timeout) throws TGStartException {

    if (this.configFile == null)
        throw new TGStartException("TGServer - Config file not set");

    if (this.logFile == null)
        this.setLogFile("tgdb_" + this.dbName);

    //this.outStream = new ByteArrayOutputStream(); // reset
    //this.errStream = new ByteArrayOutputStream(); // reset
    PumpStreamHandler psh = new PumpStreamHandler(new ByteArrayOutputStream());
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

    Executor tgExec = new DefaultExecutor();
    tgExec.setWorkingDirectory(new File(this.home + "/bin"));
    tgExec.setStreamHandler(psh);
    CommandLine tgCL = new CommandLine((new File(this.home + "/bin/" + process)).getAbsolutePath());
    tgCL.addArguments(new String[] { "-s", "-c", this.configFile.getAbsolutePath(), "-l", this.logFileBase });

    System.out.println("TGServer - Starting " + StringUtils.toString(tgCL.toStrings(), " "));
    try {
        tgExec.execute(tgCL, resultHandler);
    } catch (IOException ioe) {
        try {
            Thread.sleep(1000); // Make sure output/error fill up
        } catch (InterruptedException ie) {
            ;
        }
        throw new TGStartException(ioe.getMessage());
    }

    if (timeout > 0) {
        Calendar future = Calendar.getInstance();
        future.add(Calendar.MILLISECOND, (int) timeout);
        boolean started = false;
        boolean error = false;
        List<String> acceptedClients = new ArrayList<String>();
        String acceptedClient;
        while (!future.before(Calendar.getInstance())) {
            try {
                Thread.sleep(1000);
                BufferedReader reader = new BufferedReader(new StringReader(this.getOutput()));
                String line = reader.readLine();
                while (line != null) {
                    if (line.contains("Process pid:"))
                        this.setPid(Integer.parseInt(line.substring(line.lastIndexOf("Process pid:") + 12,
                                line.indexOf(",", line.lastIndexOf("Process pid:") + 12))));
                    if (line.contains("[Error]")) {
                        error = true;
                    }
                    if (line.contains("Accepting clients on")) {
                        started = true;
                        acceptedClient = line.substring(line.indexOf("- Accepting clients on") + 2);
                        if (!acceptedClients.contains(acceptedClient))
                            acceptedClients.add(acceptedClient);
                    }
                    line = reader.readLine();
                }
                reader.close();
                if (started)
                    break;
            } catch (Exception e) {
                throw new TGStartException("TGServer - " + e.getMessage());
            }
        }
        if (!started)
            throw new TGStartException(
                    "TGServer - Did not start on time (after " + timeout + " msec) - See log " + this.logFile);
        else {
            this.running = true;
            System.out.println("TGServer - Started successfully with pid " + this.pid + " :");
            System.out.println("\t\t- Log file: " + this.logFile);
            if (error)
                System.out.println("\t\t- With some error(s) - See log");
            for (String client : acceptedClients)
                System.out.println("\t\t- " + client);
            try {
                this.setBanner(this.getOutput());
            } catch (TGGeneralException tge) {
                throw new TGStartException(tge.getMessage());
            }
        }
    }
}

From source file:fr.fastconnect.factory.tibco.bw.maven.AbstractBWMojo.java

/**
 * This calls a TIBCO binary./*from  ww  w  . j a v  a  2 s .  c o  m*/
 * 
 * @param binary, the TIBCO binary file to execute
 * @param tras, the TRA files associated with the TIBCO binary
 * @param arguments, command-line arguments
 * @param workingDir, working directory from where the binary is launched
 * @param errorMsg, error message to display in case of a failure
 * @param fork, if true the chiild process will be detached from the caller
 * 
 * @throws IOException
 * @throws MojoExecutionException
 */
protected int launchTIBCOBinary(File binary, List<File> tras, ArrayList<String> arguments, File workingDir,
        String errorMsg, boolean fork, boolean synchronous) throws IOException, MojoExecutionException {
    Integer result = 0;

    if (tras == null) { // no value specified as Mojo parameter, we use the .tra in the same directory as the binary
        String traPathFileName = binary.getAbsolutePath();
        traPathFileName = FilenameUtils.removeExtension(traPathFileName);
        traPathFileName += ".tra";
        tras = new ArrayList<File>();
        tras.add(new File(traPathFileName));
    }

    HashMap<File, File> trasMap = new HashMap<File, File>();
    for (File tra : tras) {
        // copy of ".tra" file in the working directory
        File tmpTRAFile = new File(directory, tra.getName());
        trasMap.put(tra, tmpTRAFile);
        copyFile(tra, tmpTRAFile);
    }

    for (File tra : trasMap.keySet()) {
        if (trasMap.containsKey(tibcoDesignerTRAPath)
                && ((tibcoBuildEARUseDesignerTRA && tra == tibcoBuildEARTRAPath)
                        || (tibcoBuildLibraryUseDesignerTRA && tra == tibcoBuildLibraryTRAPath))) {
            if (tras.size() > 1) {
                ReplaceRegExp replaceRegExp = new ReplaceRegExp();
                replaceRegExp.setFile(trasMap.get(tra));
                replaceRegExp.setMatch("tibco.include.tra (.*/designer.tra)");
                replaceRegExp.setReplace(
                        "tibco.include.tra " + trasMap.get(tibcoDesignerTRAPath).toString().replace('\\', '/'));
                replaceRegExp.setByLine(true);

                replaceRegExp.execute();
            }
        }

        if (tra == tibcoBuildEARTRAPath || tra == tibcoDesignerTRAPath || tra == tibcoBWEngineTRAPath) { // FIXME: should check more properly
            // append user.home at the end to force the use of custom Designer5.prefs
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(trasMap.get(tra), true)));
            out.println("");
            out.println("java.property.user.home=" + directory.getAbsolutePath().replace("\\", "/"));
            out.close();
        }
    }

    CommandLine cmdLine = new CommandLine(binary);

    for (String argument : arguments) {
        cmdLine.addArgument(argument);
    }
    getLog().debug("launchTIBCOBinary command line : " + cmdLine.toString());
    getLog().debug("working dir : " + workingDir);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(workingDir);

    if (timeOut > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeOut * 1000);
        executor.setWatchdog(watchdog);
    }

    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());

    ByteArrayOutputStream stdOutAndErr = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(stdOutAndErr));

    if (fork) {
        CommandLauncher commandLauncher = CommandLauncherFactory.createVMLauncher();
        commandLauncher.exec(cmdLine, null, workingDir);
    } else {
        try {
            if (synchronous) {
                result = executor.execute(cmdLine);
            } else {
                executor.execute(cmdLine, new DefaultExecuteResultHandler());
            }
        } catch (ExecuteException e) {
            // TODO : grer erreurs des excutables (ventuellement parser les erreurs classiques)
            getLog().info(cmdLine.toString());
            getLog().info(stdOutAndErr.toString());
            getLog().info(result.toString());
            throw new MojoExecutionException(errorMsg, e);
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

    return result;
}

From source file:gov.nasa.jpl.magicdraw.projectUsageIntegrity.graph.SSCAEProjectUsageGraph.java

public BufferedImageFile convertDOTFile(@Nonnull File pugDOT, @Nonnull DOTImageFormat dotImageFormat)
        throws IIOException, IOException, InterruptedException {
    String dotCommand = ProjectUsageIntegrityPlugin.getInstance().getDOTexecutablePath();
    if (null == dotCommand)
        return null;

    File pugTemp = pugDOT.getParentFile();
    File pugImage = new File(pugTemp.getAbsoluteFile() + File.separator + project.getID() + "."
            + DOTImageFormatName.get(dotImageFormat));
    if (pugImage.exists()) {
        pluginLog.info(String.format("%s - convertDOTFile - deleting previous image for '%s' : '%s'",
                pluginName, project.getName(), pugImage.getName()));
        pugImage.delete();/*from   w w  w  .  j  av a  2s  .c  o m*/
    }

    CommandLine cmdLine = new CommandLine(dotCommand);
    cmdLine.addArgument("-Tpng");
    cmdLine.addArgument("-o");
    cmdLine.addArgument(pugImage.getName());
    cmdLine.addArgument(pugDOT.getName());

    pluginLog.info(String.format("%s - convertDOTgraph - converting gv to image for '%s'", pluginName,
            project.getName()));

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

    ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000);

    // consider '0' exit value as success.
    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.setWatchdog(watchdog);
    executor.setWorkingDirectory(pugTemp);
    executor.execute(cmdLine, resultHandler);

    resultHandler.waitFor();

    if (!executor.isFailure(resultHandler.getExitValue())) {
        pluginLog.info(String.format("%s - convertDOTgraph - reading image for '%s' from: '%s'", pluginName,
                project.getName(), pugImage.getName()));
        BufferedImageFile imageFile = new BufferedImageFile(pugImage);

        pluginLog.info(
                String.format("%s - convertDOTgraph - got image for '%s'", pluginName, project.getName()));
        return imageFile;
    }

    return null;
}

From source file:gov.nasa.jpl.magicdraw.projectUsageIntegrity.graph.SSCAEProjectUsageGraph.java

/**
 * @param pugDOT gv file//from w w w .j  a  v a 2 s.  co  m
 * @return true if the graphviz application was opened successfully for the gv file.
 * @throws IIOException
 * @throws IOException
 * @throws InterruptedException
 */
public boolean openDOTFileWithGraphViz(@Nonnull File pugDOT)
        throws IIOException, IOException, InterruptedException {
    String graphvizApp = ProjectUsageIntegrityPlugin.getInstance().getGraphvizApplicationPath();
    if (null == graphvizApp)
        return false;

    File pugTemp = pugDOT.getParentFile();

    CommandLine cmdLine;

    switch (SSCAEProjectUsageIntegrityOptions.getCurrentPlatform()) {
    case LINUX:
        cmdLine = new CommandLine(graphvizApp);
        break;
    case MACOSX:
        cmdLine = new CommandLine("/usr/bin/open");
        cmdLine.addArgument("-a");
        cmdLine.addArgument(graphvizApp);
        break;
    case WINDOWS:
        cmdLine = new CommandLine("cmd");
        cmdLine.addArgument("/c");
        cmdLine.addArgument("start");
        cmdLine.addArgument(graphvizApp);
        break;
    default:
        return false;
    }
    cmdLine.addArgument(pugDOT.getName());

    pluginLog.info(String.format("%s - openDOTFileWithGraphViz - opening DOT file for project: '%s'",
            pluginName, project.getName()));

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

    ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000);

    // consider '0' exit value as success.
    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.setWatchdog(watchdog);
    executor.setWorkingDirectory(pugTemp);
    executor.execute(cmdLine, resultHandler);

    resultHandler.waitFor();

    if (executor.isFailure(resultHandler.getExitValue())) {
        pluginLog.error(String.format(
                "%s - openDOTFileWithGraphViz - error while opening DOT file for project '%s' from: '%s'",
                pluginName, project.getName(), pugDOT.getAbsolutePath()), resultHandler.getException());
        return false;
    }

    pluginLog.info(String.format("%s - openDOTFileWithGraphViz - opened DOT file for project '%s' from: '%s'",
            pluginName, project.getName(), pugDOT.getAbsolutePath()));
    return true;
}

From source file:net.test.aliyun.z7.Zip7Object.java

public static int extract(final String extToosHome, final String extractFile, final String toDir)
        throws Throwable {
    String cmdLine = String.format("%s\\7z.exe x \"%s\" -aoa -y \"-o%s\"", extToosHome, extractFile, toDir);
    CommandLine commandline = CommandLine.parse(cmdLine);
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    DefaultExecutor executor = new DefaultExecutor();
    int extValue = -1;
    try {//from w  ww .  j  a v a 2  s . c  o m
        executor.execute(commandline, resultHandler);
        resultHandler.waitFor(300 * 1000);
        extValue = resultHandler.getExitValue();
        if (extValue == 0) {
            new File(extractFile).delete();
        }
    } catch (Exception e) {
        //
    } finally {
        if (extValue != 0) {
            FileUtils.deleteDir(new File(toDir));
            return extValue;
        }
    }
    //
    Iterator<File> itFile = FileUtils.iterateFiles(new File(toDir), FileFilterUtils.fileFileFilter(),
            FileFilterUtils.directoryFileFilter());
    while (itFile.hasNext()) {
        File it = itFile.next();
        if (it.isDirectory())
            continue;
        for (String com : compression) {
            if (StringUtils.endsWithIgnoreCase(it.getName(), com)) {
                String itPath = it.getAbsolutePath();
                String subToDir = itPath.substring(0, itPath.length() - com.length());
                extract(extToosHome, itPath, subToDir);
            }
        }
    }
    return 0;
}

From source file:org.apache.bigtop.itest.hive.HiveHelper.java

public static Map<String, String> execCommand(CommandLine commandline, Map<String, String> envVars) {

    System.out.println("Executing command:");
    System.out.println(commandline.toString());
    Map<String, String> env = null;
    Map<String, String> entry = new HashMap<String, String>();
    try {//from  www.  j a  v  a  2  s.  c om
        env = EnvironmentUtils.getProcEnvironment();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        LOG.debug("Failed to get process environment: " + e1.getMessage());
        e1.printStackTrace();
    }
    if (envVars != null) {
        for (String key : envVars.keySet()) {
            env.put(key, envVars.get(key));
        }
    }

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 10000);
    Executor executor = new DefaultExecutor();
    executor.setExitValue(1);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(streamHandler);
    try {
        executor.execute(commandline, env, resultHandler);
    } catch (ExecuteException e) {
        // TODO Auto-generated catch block
        LOG.debug("Failed to execute command with exit value: " + String.valueOf(resultHandler.getExitValue()));
        LOG.debug("outputStream: " + outputStream.toString());
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString() + e.getMessage());
        e.printStackTrace();
        return entry;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        LOG.debug("Failed to execute command with exit value: " + String.valueOf(resultHandler.getExitValue()));
        LOG.debug("outputStream: " + outputStream.toString());
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString() + e.getMessage());
        e.printStackTrace();
        return entry;
    }

    try {
        resultHandler.waitFor();
        /*System.out.println("Command output: "+outputStream.toString());*/
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString());
        return entry;
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        /*System.out.println("Command output: "+outputStream.toString());*/
        LOG.debug("exitValue: " + String.valueOf(resultHandler.getExitValue()));
        LOG.debug("outputStream: " + outputStream.toString());
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString());
        e.printStackTrace();
        return entry;
    }
}

From source file:org.apache.karaf.decanter.kibana6.KibanaController.java

public KibanaController(File workingDirectory) {
    this.workingDirectory = workingDirectory;
    this.workingDirectory.mkdirs();
    this.executor = new DaemonExecutor();
    PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(new LogOutputStream() {
        @Override//from  w  w  w .j a v  a2  s.c om
        protected void processLine(String line, int logLevel) {
            KIBANA_LOGGER.info(line);
        }
    });
    executor.setStreamHandler(pumpStreamHandler);
    executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
    executeResultHandler = new DefaultExecuteResultHandler();
}

From source file:org.apache.zeppelin.submarine.job.thread.JobRunThread.java

public void run() {
    boolean tryLock = lockRunning.tryLock();
    if (false == tryLock) {
        LOGGER.warn("Can not get JobRunThread lockRunning!");
        return;/*from  w  w  w  .  j  a v a2s.co m*/
    }

    SubmarineUI submarineUI = submarineJob.getSubmarineUI();
    try {
        InterpreterContext intpContext = submarineJob.getIntpContext();
        String noteId = intpContext.getNoteId();
        String userName = intpContext.getAuthenticationInfo().getUser();
        String jobName = SubmarineUtils.getJobName(userName, noteId);

        if (true == running.get()) {
            String message = String.format("Job %s already running.", jobName);
            submarineUI.outputLog("WARN", message);
            LOGGER.warn(message);
            return;
        }
        running.set(true);

        Properties properties = submarineJob.getProperties();
        HdfsClient hdfsClient = submarineJob.getHdfsClient();
        File pythonWorkDir = submarineJob.getPythonWorkDir();

        submarineJob.setCurrentJobState(EXECUTE_SUBMARINE);

        String algorithmPath = properties.getProperty(SubmarineConstants.SUBMARINE_ALGORITHM_HDFS_PATH, "");
        if (!algorithmPath.startsWith("hdfs://")) {
            String message = "Algorithm file upload HDFS path, " + "Must be `hdfs://` prefix. now setting "
                    + algorithmPath;
            submarineUI.outputLog("Configuration error", message);
            return;
        }

        List<ParagraphInfo> paragraphInfos = intpContext.getIntpEventClient().getParagraphList(userName,
                noteId);
        String outputMsg = hdfsClient.saveParagraphToFiles(noteId, paragraphInfos,
                pythonWorkDir == null ? "" : pythonWorkDir.getAbsolutePath(), properties);
        if (!StringUtils.isEmpty(outputMsg)) {
            submarineUI.outputLog("Save algorithm file", outputMsg);
        }

        HashMap jinjaParams = SubmarineUtils.propertiesToJinjaParams(properties, submarineJob, true);

        URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_JOBRUN_TF_JINJA);
        String template = Resources.toString(urlTemplate, Charsets.UTF_8);
        Jinjava jinjava = new Jinjava();
        String submarineCmd = jinjava.render(template, jinjaParams);
        // If the first line is a newline, delete the newline
        int firstLineIsNewline = submarineCmd.indexOf("\n");
        if (firstLineIsNewline == 0) {
            submarineCmd = submarineCmd.replaceFirst("\n", "");
        }

        StringBuffer sbLogs = new StringBuffer(submarineCmd);
        submarineUI.outputLog("Submarine submit command", sbLogs.toString());

        long timeout = Long
                .valueOf(properties.getProperty(SubmarineJob.TIMEOUT_PROPERTY, SubmarineJob.defaultTimeout));
        CommandLine cmdLine = CommandLine.parse(SubmarineJob.shell);
        cmdLine.addArgument(submarineCmd, false);
        DefaultExecutor executor = new DefaultExecutor();
        ExecuteWatchdog watchDog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchDog);
        StringBuffer sbLogOutput = new StringBuffer();
        executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
            @Override
            protected void processLine(String line, int level) {
                line = line.trim();
                if (!StringUtils.isEmpty(line)) {
                    sbLogOutput.append(line + "\n");
                }
            }
        }));

        if (Boolean.valueOf(properties.getProperty(SubmarineJob.DIRECTORY_USER_HOME))) {
            executor.setWorkingDirectory(new File(System.getProperty("user.home")));
        }

        Map<String, String> env = new HashMap<>();
        String launchMode = (String) jinjaParams.get(SubmarineConstants.INTERPRETER_LAUNCH_MODE);
        if (StringUtils.equals(launchMode, "yarn")) {
            // Set environment variables in the submarine interpreter container run on yarn
            String javaHome, hadoopHome, hadoopConf;
            javaHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_JAVA_HOME);
            hadoopHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_HADOOP_HDFS_HOME);
            hadoopConf = (String) jinjaParams.get(SubmarineConstants.SUBMARINE_HADOOP_CONF_DIR);
            env.put("JAVA_HOME", javaHome);
            env.put("HADOOP_HOME", hadoopHome);
            env.put("HADOOP_HDFS_HOME", hadoopHome);
            env.put("HADOOP_CONF_DIR", hadoopConf);
            env.put("YARN_CONF_DIR", hadoopConf);
            env.put("CLASSPATH", "`$HADOOP_HDFS_HOME/bin/hadoop classpath --glob`");
            env.put("ZEPPELIN_FORCE_STOP", "true");
        }

        LOGGER.info("Execute EVN: {}, Command: {} ", env.toString(), submarineCmd);
        AtomicBoolean cmdLineRunning = new AtomicBoolean(true);
        executor.execute(cmdLine, env, new DefaultExecuteResultHandler() {
            @Override
            public void onProcessComplete(int exitValue) {
                String message = String.format("jobName %s ProcessComplete exit value is : %d", jobName,
                        exitValue);
                LOGGER.info(message);
                submarineUI.outputLog("JOR RUN COMPLETE", message);
                cmdLineRunning.set(false);
                submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_FINISHED);
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                String message = String.format("jobName %s ProcessFailed exit value is : %d, exception is : %s",
                        jobName, e.getExitValue(), e.getMessage());
                LOGGER.error(message);
                submarineUI.outputLog("JOR RUN FAILED", message);
                cmdLineRunning.set(false);
                submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR);
            }
        });
        int loopCount = 100;
        while ((loopCount-- > 0) && cmdLineRunning.get() && running.get()) {
            Thread.sleep(1000);
        }
        if (watchDog.isWatching()) {
            watchDog.destroyProcess();
            Thread.sleep(1000);
        }
        if (watchDog.isWatching()) {
            watchDog.killedProcess();
        }

        // Check if it has been submitted to YARN
        Map<String, Object> jobState = submarineJob.getJobStateByYarn(jobName);
        loopCount = 50;
        while ((loopCount-- > 0) && !jobState.containsKey("state") && running.get()) {
            Thread.sleep(3000);
            jobState = submarineJob.getJobStateByYarn(jobName);
        }

        if (!jobState.containsKey("state")) {
            String message = String.format("JOB %s was not submitted to YARN!", jobName);
            LOGGER.error(message);
            submarineUI.outputLog("JOR RUN FAILED", message);
            submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR);
        submarineUI.outputLog("Exception", e.getMessage());
    } finally {
        running.set(false);
        lockRunning.unlock();
    }
}