use of org.apache.commons.exec.DefaultExecutor in project jaqy by Teradata.
the class Os method shell.
public void shell(File dir, String cmd) throws Exception {
CommandLine commandLine;
if (s_windows) {
commandLine = new CommandLine("cmd");
commandLine.addArgument("/C");
commandLine.addArgument(cmd, false);
} else {
commandLine = new CommandLine("/bin/bash");
commandLine.addArgument("-c");
commandLine.addArgument(cmd, false);
}
Executor executor = new DefaultExecutor();
executor.setWorkingDirectory(dir);
executor.setStreamHandler(new PumpStreamHandler(System.out, System.out));
executor.execute(commandLine);
}
use of org.apache.commons.exec.DefaultExecutor in project xwiki-platform by xwiki.
the class XWikiExecutor method executeCommand.
private DefaultExecuteResultHandler executeCommand(String commandLine) throws Exception {
// The command line to execute
CommandLine command = CommandLine.parse(commandLine);
// Execute the process asynchronously so that we don't block.
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
// Send Process output and error streams to our logger.
PumpStreamHandler streamHandler = new PumpStreamHandler(new XWikiLogOutputStream(XWikiLogOutputStream.STDOUT), new XWikiLogOutputStream(XWikiLogOutputStream.STDERR));
// Make sure we end the process when the JVM exits
ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
// Prevent the process from running indefinitely and kill it after 1 hour...
ExecuteWatchdog watchDog = new ExecuteWatchdog(60L * 60L * 1000L);
// The executor to execute the command
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(streamHandler);
executor.setWorkingDirectory(new File(getExecutionDirectory()));
executor.setProcessDestroyer(processDestroyer);
executor.setWatchdog(watchDog);
// Inherit the current process's environment variables and add the user-defined ones
@SuppressWarnings("unchecked") Map<String, String> newEnvironment = EnvironmentUtils.getProcEnvironment();
newEnvironment.putAll(this.environment);
executor.execute(command, newEnvironment, resultHandler);
return resultHandler;
}
use of org.apache.commons.exec.DefaultExecutor in project xxl-job by xuxueli.
the class ScriptUtil method execToFile.
/**
* 日志文件输出方式
*
* 优点:支持将目标数据实时输出到指定日志文件中去
* 缺点:
* 标准输出和错误输出优先级固定,可能和脚本中顺序不一致
* Java无法实时获取
*
* @param command
* @param scriptFile
* @param logFile
* @param params
* @return
* @throws IOException
*/
public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
// 标准输入
try (FileOutputStream fileOutputStream = new FileOutputStream(logFile, true)) {
PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);
// command
CommandLine commandline = new CommandLine(command);
commandline.addArgument(scriptFile);
if (params != null && params.length > 0) {
commandline.addArguments(params);
}
// exec
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
exec.setStreamHandler(streamHandler);
// exit code: 0=success, 1=error
int exitValue = exec.execute(commandline);
return exitValue;
}
}
use of org.apache.commons.exec.DefaultExecutor in project scala-maven-plugin by davidB.
the class JavaMainCallerByFork method run.
@Override
public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
List<String> cmd = buildCommand();
displayCmd(displayCmd, cmd);
Executor exec = new DefaultExecutor();
// err and out are redirected to out
if (!_redirectToLog) {
exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in));
} else {
exec.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
private LevelState _previous = new LevelState();
@Override
protected void processLine(String line, int level) {
try {
_previous = LogProcessorUtils.levelStateOf(line, _previous);
switch(_previous.level) {
case ERROR:
requester.getLog().error(line);
break;
case WARNING:
requester.getLog().warn(line);
break;
default:
requester.getLog().info(line);
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}));
}
CommandLine cl = new CommandLine(cmd.get(0));
for (int i = 1; i < cmd.size(); i++) {
cl.addArgument(cmd.get(i), false);
}
try {
int exitValue = exec.execute(cl);
if (exitValue != 0) {
if (throwFailure) {
throw new MojoFailureException("command line returned non-zero value:" + exitValue);
}
return false;
}
if (!displayCmd)
tryDeleteArgFile(cmd);
return true;
} catch (ExecuteException exc) {
if (throwFailure) {
throw exc;
}
return false;
}
}
use of org.apache.commons.exec.DefaultExecutor in project zalenium by zalando.
the class CommonProxyUtilities method convertFlvFileToMP4.
public void convertFlvFileToMP4(TestInformation testInformation) {
// Names of the old and the new file
String flvVideoFile = testInformation.getVideoFolderPath() + "/" + testInformation.getFileName();
testInformation.setFileExtension(".mp4");
testInformation.buildVideoFileName();
String mp4VideoFile = testInformation.getVideoFolderPath() + "/" + testInformation.getFileName();
// Command to convert the file to MP4
CommandLine commandLine = new CommandLine("ffmpeg");
commandLine.addArgument("-i");
commandLine.addArgument(flvVideoFile);
commandLine.addArgument(mp4VideoFile);
DefaultExecutor defaultExecutor = new DefaultExecutor();
ExecuteWatchdog executeWatchdog = new ExecuteWatchdog(10 * 1000);
defaultExecutor.setWatchdog(executeWatchdog);
try {
int exitValue = defaultExecutor.execute(commandLine);
if (exitValue != 0) {
LOG.warn("File " + flvVideoFile + " could not be converted to MP4. Exit value: " + exitValue);
}
} catch (IOException e) {
LOG.error(e.toString(), e);
}
// Deleting the FLV file
FileUtils.deleteQuietly(new File(flvVideoFile));
}
Aggregations