I'm using filebeat to read some log files and I need to start filebeat using a Java program. And the filebeat commands are executed using the Windows PowerShell. I used the following code but it didn't work.
try {
ProcessBuilder b1 = new ProcessBuilder("powershell.exe", "/c", "cd \"C:\\Program Files\\Filebeat\" && ./filebeat -e -c filebeat.yml -d \"publish\"\\");
b1.redirectErrorStream(true);
Process p1 = b1.start();
BufferedReader r1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
String line1;
while (true) {
line1 = r1.readLine();
if (line1 == null) { break; }
System.out.println(line1);
}
} catch(Exception e) {
}
The below code worked.
ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Filebeat\\filebeat.exe", "-c", "C:\\Program Files\\Filebeat\\filebeat.yml", "-e");
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
You're confusing PowerShell with CMD. The latter has a parameter /c, the former doesn't. Also, instead of using cd in the comandline you should simply set the working directory on the ProcessBuilder object, and each token of your commandline should be a separate array element.
Change your code to something like this and it should do what you expect:
ProcessBuilder b1 = new ProcessBuilder("cmd.exe", "/c", "filebeat", "-e", "-c", "filebeat.yml", "-d", "\"publish\"");
b1.directory(new File("C:\\Program Files\\Filebeat"));
b1.redirectErrorStream(true);
Process p1 = b1.start();
Related
I'm having a challenge with sudo invoked commands using both ProcessBuilder and Runtime.exec. I am thinking that ProcessBuilder is overall the better solution but both produce the same result - they execute shell commands fine on Ubuntu, but if I try to do a sudo -i mysql command for example:
public static void runProcess(String[] process) {
String s = null;
try {
Process p = new ProcessBuilder(process).start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) { System.out.println(s); }
while ((s = stdError.readLine()) != null) { System.out.println(s); }
} catch (IOException e) { e.printStackTrace(); }
}
These 2 commands work:
String[] cmdArgs0 = { "sudo", "echo", "Done" };
runProcess(cmdArgs0);
String[] cmdArgs1 = { "bash", usbDrivePath+"/ASWebUI/Install.sh" };
runProcess(cmdArgs1);
But this does not:
String[] cmdArgs2 = { "sudo", "-i", "mysqldump", "Core", ">", cachePath+"/SQLDumps/Core.sql" };
runProcess(cmdArgs2);
Error:
mesg: ttyname failed: Inappropriate ioctl for device
mysqldump: Couldn't find table: ">"
ProcessBuilder doesn't allow you to redirect the output using > character. Instead you can use processBuilder.redirectOutput() method to specify the desired output.
File dumpFile = new File("Core.sql");
processBuilder.redirectOutput(Redirect.to(dumpFile));
Or even use --result-file option of mysqldump to specify the dump file:
mysqldump [options] --result-file=dump.sql
I'm using logstash to extract data from log files. I wish to call logstash from a java program. How can I perform such task?
This code worked.
ProcessBuilder b1 = new ProcessBuilder("cmd.exe", "/c", "cd \"C:\\elk\\logstash-5.1.2\\bin\" && logstash -f first-pipeline.conf --config.reload.automatic");
b1.redirectErrorStream(true);
Process p1 = b1.start();
BufferedReader r1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
String line1;
while (true) {
line1 = r1.readLine();
if (line1 == null) { break; }
System.out.println(line1);
}
I am trying to execute a bash script from Java with ProcessBuilder
my code is :
Process createUser = buildProcess(
"/bin/su",
"-c",
"\"/opt/somedir/testdir/current/bin/psql",
"--command",
commandForUserCreation,
/* "'select * from users'", */
"--dbname",
"mydbname\"",
"myuser"
);
The problem is that I receive error:
/bin/su: unrecognized option '--dbname'
If I put echo in first place of my commands it prints correct command in bash and if I copy/paste this command it works!
Please, help me to resolve this issue.
You need to supply the whole command to execute by su as a single argument. Try this:
Process createUser = buildProcess(
"/bin/su",
"-c",
"/opt/vmware/vpostgres/current/bin/psql --command " + commandForUserCreation + " --dbname mydbname",
myuser
);
This is what I use in processBuilder:
String[] command = new String[] {"echo", "Hello"};
String workspace = "/bin/su";
System.out.println("Trying to run command: "+ Arrays.toString(command));
ProcessBuilder probuilder = new ProcessBuilder(command);
probuilder.directory(new File(workspace));
Process process = probuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:\n",Arrays.toString(command));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
I hope it helps.
How to run cd command(In linux Ubuntu) and ffmpeg on the changed directory. The following jsp program not working for me.
String cmd = "cd "+getServletContext().getRealPath("/")+"Files/videos/";
out.println(cmd);
ProcessBuilder pb = new ProcessBuilder(
"/bin/sh", "-c",
cmd + "&& ffmpeg -i nature.MP4");
Process p = pb.start();
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()) );
String line;
out.println("Meta-data...");
while ((line = in.readLine()) != null) {
out.println(line);
}
in.close();
Thanks in advance...
Here is the unix command for adding a file to the queue.
enq -P QueueName:PrinterName FileName
Is it possible to run the above command using java.
Yes, it's possible using ProcessBuilder:
ProcessBuilder builder =
new ProcessBuilder("enq", "-P", "QueueName", "FileName");
Process process = builder.start();
InputStreamReader streamReader = new InputStreamReader(process.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
See: enq syntax
Process p = Runtime.getRuntime().exec(new String[]{"enq", "-P", "QueueName:PrinterName FileName"});