I am running wkhtmltopdf from Java. I create a process but this one seems to be hanging, as it repeats again and again the the console, also in the Task Manager.
This is how I run wkhtmltopdf:
String command = applicationLocation + "wkhtmltopdf.exe -O Landscape " + reqURL + "?" + reqQuery + " c:/PDF/" + folderName + "/" + id + "/" + folderName + ".pdf";
Process p = Runtime.getRuntime().exec(command);
How can I "destroy" the process, after the job has been done?
This did not work for me, the process never stopped and the code never entered the while loop either:
ProcessBuilder pb = new ProcessBuilder(application, htmlFilePath, pdfFilePath);
Process process = pb.start();
BufferedReader errStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
System.out.println("read errstreamreader");
//not "process.getInputStream()"
String line = null;
line = errStreamReader.readLine();
while(line != null) {
System.out.println(line);
line = errStreamReader.readLine();
if(line.equals("Done")) {
process.destroy();
System.out.println("destroyed process");
}
}
That was a very ugly problem. My code caused the loop. I called this class which generates the PDF from a servlet with this code:
// create pdf
if(action != null && action.equals("pdf")) {
String reqURL = request.getRequestURL().toString();
String reqQuery = "id=" + bomModuleId+ "&action=pdf";
String folderName = "doonot";
GeneratePDF obj = new GeneratePDF();
obj.genrateCmd(reqURL, "xxx", "xxx", reqQuery, folderName, "10.07.2012");
}
It turned out, that wkhtmltopdf used exactely the same URL, so it made a request to that page, landed in this loop, and called wkhtmltopdf again. So I ended up in like 450 wkhtmltopdf processes, and everything crashed.
Solution: I removed "&action=pdf"!
As it turns out i had the same problem too: looping wkhtmltopdf creates a bunch of processes causing my machine,running Windows 7, to drastically slow down.
The problem isnt really with wkhtmltopdf as it is with Runtime exec. It creates more wkhtmltopdf processes without waiting for any of the previous to finish until the lack of the system's resources freezes your computer.
My Solution: add p.waitFor(). It will wait for the wkhtmltopdf process to finish and then continue the loop.
This solution can still be slow because now a large html file can slow down the completion of your loop. So, if you want a still faster solution I suggest using the --read-from-stdin option from wkhtmltopdf.
As for reading the output of wkhtmltopdf I suggest looking at this question and ALL of its answers.
Related
I'm writing a wrapper for the command line based headless server of the game Factorio. I'm using a ProcessBuilder and getting the stdout of the server and am using the rcon connection (long story, stdin wasn't working) to communicate with it. I've gotten it nearly completed up until the point where I was going to package up the jar and run it on the physical server. This is when I noticed that running the jar instead of running from the IDE (Intelij) prevents any output from the server from coming through. I did some more snooping and found that whenever I am using .getInputStream() from the process, the server's output is only sent if the program was launched alongisde the console (java.exe) and doesn't get sent at all if the program was launched without it (javaw.exe).
I tested .getInputStream() with cmd.exe and it works just fine with both java.exe and javaw.exe. I also check to see if the code was following some flow that I wasn't expecting when ran outside of the IDE but it blocks on read.readLine() as if it's waiting for input but not receiving anything.
The process is initialized here:
ProcessBuilder pb = new ProcessBuilder(gameExecutablePath, "--start-server", saveLocation + "\\" + saveName, "--server-settings", serverSettingsLocation + "\\" + serverSettingsName, "-c", serverConfigLocation + "\\" + serverConfigName, "--rcon-port", "" + rconPort, "--rcon-password", "" + rconPasskey);
pb.redirectErrorStream(true);
try
{
myProcess = pb.start();
}catch(Exception e){e.printStackTrace();}
if(myProcess == null) { stop();}
OutputStream serverInput = myProcess.getOutputStream();
write = new BufferedWriter(new OutputStreamWriter(serverInput));
InputStream serverOutput = myProcess.getInputStream();
read = new BufferedReader(new InputStreamReader(serverOutput));
Later, the input is handled by the gui and is sent via rcon (after the server has been fully initialized), and the output is read and printed to the gui's feed, JTextArea, here:
while(serverRunning)
{
try
{
String line = "" + read.readLine();
if(line == null)
{
continue;
}
line = line.trim();
if(line.contains("Opening socket for broadcast"))
{
serverInitialized = true;
initializeRCONConnection();
}
synchronized(serverFeedStringBuilder)
{
printToServerFeed(line);
}
}catch(Exception e){e.printStackTrace();printToServerFeed(e.getMessage());}
}
The application should have printed out the server's output such as all the initializing text about modloading, connecting to the matchmaking server, etc. like it does in the IDE and when java.exe is used. Instead it doesn't output anything at all but the process continues to run and the server can be connected to in-game after it's done initializing.
I have a webappliaction(developed in Jsp & Severlet) that execute the sh script and displays the output in browser only after executing the script but i want to print the output like how its executing in teriminal (one by one for eg ping command).So that user will have the experience like working in unix terminal. My script will run almost one minute(Script to start stop my WAS servers) so the user should not wait till one minute to see the final output . They should see the script started output once they start the process .please find my sample code below.
pb = new ProcessBuilder("/bin/sh",script);
pb.directory(new File(filePath));
p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null){
System.out.println(line);
out.println(line);
}
p.waitFor();
System.out.println ("exit: " + p.exitValue());
out.println("exit: " + p.exitValue());
p.destroy();
out.println("Script Executed");
Please anyone guide me.
Finally i got a solution for my problem, I just added out.flush() after out.println(line);So its flushing output to browser each time in while loop and its looks like unix terminal . Below code did the magic.
pb = new ProcessBuilder("/bin/sh",script);
pb.directory(new File(filePath));
p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null){
System.out.println(line);
out.println(line);
out.flush();
}
p.waitFor();
System.out.println ("exit: " + p.exitValue());
out.println("exit: " + p.exitValue());
p.destroy();
out.println("Script Executed");
Your problem is invisible here in the code example because it works just fine locally (e.g. for System.out). So logic implies that the problem must be somewhere in networking between client and server. Neither do you show how out is created (I guess it is some kind of socket-connected PrintStream) nor how the JSP reads it. What you want is some AJAX approach (XmlHttpRequest or similar) and probably you do not use it, but some "naïve", old-fashioned way. I am not a web developer, but your favourite search engine or some other people here might be able to help you with that part.
Here is my workflow:
I get job from DB, I run a few tasks, I run an external program that reads a file and produces another one (this usually takes under 10 seconds). Here is the code:
Process p = Runtime.getRuntime().exec(prog, null, new File(path));
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s;
String errorString = "";
while((s = stdInput.readLine()) != null) {
if(s.toLowerCase().contains("error")) {
Log.writeLog("error: " + s);
errorString += s + "\r\n";
}
}
if(errorString.length() > 1) {
Emailer.email(name + "*" + errorString, "ERROR");
}
while((s = stdError.readLine()) != null) {
Log.writeLog("ERROR: " + s);
}
However, the snippet hanged. I control the server that the code runs on through LogMeIn, once I logged in, the process unblocked (total running time around 280 seconds) and continued. The process did not produce an ERROR results. This happens from time to time more often than we would like to. We do quite a bit of small IOs operation in the program and the harddrive gets pretty full from time to time.
Any idea what could be happening?
Thanks!
EDIT: the server is a just a regular computer that is connected to LogMeIn. My fear is that since it is a regular computer, it may powerdown the CPU/hard drive when not in use (not sure the correct terminology). This would somewhat explain why it would continue once I logged in to LogMeIn and had access to a computer.
EDIT2: directly following the process, I run this. And this hangs for an absurd amount of time as well (usually 5 seconds, took 200+ seconds). Makes me thing that the hard drive is decided to take a nap?
private void cleanup(String path) {
File srcPath = new File(path);
File[] files = srcPath.listFiles();
if(files != null) {
for(File file : files) {
if(file.isDirectory()) {
cleanup(file.getAbsolutePath());
} else {
if(file.getAbsolutePath().endsWith(".original")) {
String fileName = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".original"));
IO.delete(fileName);
if(!IO.renameFile(file.getAbsolutePath(), new File(fileName).getAbsolutePath())) {
Log.writeLogSevere("Failed to rename file, this could be a problem..." + fileName);
} else {
Log.writeLog("Cleaned up: " + fileName);
}
}
}
}
}
}
You are not draining the error stream. You do it at the end, which may often be too late. The output buffer of the process fills up and the process blocks waiting to get more space in the stderr output buffer.
You must either use a separate thread for that, or (much simpler) redirectErrorStream using ProcessBuilder.
The most likely thing is that the thread running p didn't die and p.getInputStream() is not null, and but not data on it.
While it's hanging, I would check the current running processes (ps command on Unix) or Task manager on windows. This will tell you if p is done or not. If it is not, then whatever that program is, has issues and it's holding up the rest of your code.
I'm using mencoder to split files and I'd like to turn this into an Object Oriented approach, if possible, using Java or similar, for example. But I'm not sure the best way, so I leave it in the open. Here is what I need:
I have an excel file with start times and end times, and I need to extract out the appropriate clips from a video file. In the terminal (I'm on Mac OS X) I've had success using, for example:
mencoder -ss 0 -endpos 10 MyVideo.avi -oac copy -ovc copy -o Output.avi
Which creates the video Output.avi by clipping the first 10 seconds of the video MyVideo.avi.
But, like I said, I want to make it so that a program reads in from an excel file, and calls this mencoder command multiple times (over 100) for each of the start times and end times.
I know how to read in the excel file in Java, but I'm not sure it is best to call this command from Java. Plus, I'd like to be able to see the output of mencoder (because it prints out a nice percentage so you know about how much longer a single command will take). Is this type of thing feasible to do in a shell script? I would really like to use Java if possible, since I have many years of experience in Java and no experience in shell scripting.
UPDATE
Here is what I've tried in Java, but it freezes at in.readLine()
File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd ..");
out.println("pwd");
String video = "/Users/MyFolder/MyFile.avi";
String output = "/Users/MyFolder/output.avi";
int start = 0;
int end = 6;
String cmd = "mencoder -ss " + start +
" -endpos " + end +
" " + video + " -oac copy -ovc copy -o " + output;
out.println(cmd);
try {
String line;
System.out.println("top");
while ((line = in.readLine()) != null) {
System.out.println(line);
}
System.out.println("end");
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}
I'm not quite sure about mencoders multicore-capabilities, but I think with Java you can use Multiple Threads to get the maximal power of all cpu-cores.
You shouldn't use Runtime like your using it.
When using Runtime, you should not run bash and send commands via inputstream like when you are typing commands on a terminal.
Runtime.getRuntime().exec("mencoder -ss " + start +
" -endpos " + end +
" " + video + " -oac copy -ovc copy -o " + output);
To get the Output, you can use the inputStream
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String,%20java.lang.String[],%20java.io.File%29
With this command you can also set the Workingdirectory where your command is executed.
I also prefer the version with the String[] as parameters. It's much more readable, than the a concatenated String.
In a java program, I am generating an sh script for use on a centOS machine, which will use sox and lame to decode an MP3 audio file, then apply some gain to the file respectively. Im having some issues getting the Process.waitFor() method to do anything other than hang indefinitely. Here is the code:
try
{
// TODO code application logic here
String reviewPath = "/SomeDirectory/";
String fileName = "FileName";
String extension = ".mp3";
StringBuilder sb = new StringBuilder();
sb.append("#!/bin/bash\n");
sb.append("cd " + reviewPath + "\n");
sb.append("lame --decode " + fileName + extension + "\n");
File script = new File(reviewPath + fileName + ".sh");
script.createNewFile();
script.setExecutable(true);
FileWriter writer = new FileWriter(script);
writer.write(sb.toString());
writer.close();
Process p = Runtime.getRuntime().exec(script.getAbsolutePath());
String line;
BufferedReader bri = new BufferedReader
(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader
(new InputStreamReader(p.getErrorStream()));
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null) {
System.out.println(line);
}
bre.close();
p.waitFor();
System.out.println("Done.");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
The odd part is that when I run the .sh file it generates by hand, it runs and exits nicely, but when I execute it from a process object in java, it never exits. The exitValue of the process is always "Process has not exited". Ive tried adding set -e to the script, and exit to the end of the script. Short of using the kill command (which I dont really think I can do here) Im at a loss as to what is going on here. Any suggestions?
Add something like while(p.getInputStream().read() != -1); after starting the process. The buffer will get filled and the process will stop waiting for something (in this case, your program) to read from it to free up space.
I figured it out! The problem here was indeed that the output streams needed to be flushed for the application to exit, but simply reading from the streams is not enough. I used Suresh Koya's suggestion and used the processBuilder api, and redirected the error stream on the process before starting it, and read from the streams. This fixed the issues I was having :D