Set ulimit -n from java - java

I am trying to set ulimit under which my java program runs. Currently, it seems that ulimit -n is set to 4096 because when I run this code (which is a part of my java program), it outputs 4096.
ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", "ulimit -n");
try {
Process process = processBuilder.start();
// Prints 4096.
LOGGER_.info(getStringFromInputStream(process.getInputStream()));
} catch (IOException e) {
// The flow does not reach this catch block.
LOGGER_.error("exception caught: " + e.getMessage());
}
Is it possible to change it to something else, say 8192? I tried this:
ProcessBuilder processBuilder2 = new ProcessBuilder("/bin/bash", "-c", "ulimit -n 8192");
try {
Process process = processBuilder2.start();
LOGGER_.error("starting agent2...");
LOGGER_.error(getStringFromInputStream(process.getInputStream()));
} catch (IOException e) {
LOGGER_.error("exception caught2: " + e.getMessage());
}
and
try {
String[] cmdString = new String[3];
cmdString[0] = "/bin/bash";
cmdString[1] = "-c";
cmdString[2] = "ulimit -n 8192";
Process process = Runtime.getRuntime().exec(cmdString);
LOGGER_.error(getStringFromInputStream(process.getInputStream()));
} catch (IOException e) {
LOGGER_.error("exception caught:" + e.getMessage());
}
But I am not sure if these are the correct way to do so. Also, if the ulimit -n is even getting modified or not.

ulimit command updates the limits of the current process and all inherited processes. When you invoke ulimit using Runtime.exec or ProcessBuilder, it starts a new process and updates the limits of this new process with no effect on current Java process.
In order to apply new limits on itself, Java process should call setrlimit function in its own context. Since there is no Java wrapper for this function, it can be called only via a native interface: JNI, JNA or JNR.
However, if Java runs under unprivileged user, updating file limit (ulimit -n) is useless anyway, because HotSpot JVM updates this limit to maximum allowed value automatically - see this question.

I did a workaround that works. I was using a shell script that executed the java program. So, I set the ulimit before the shell script executed the running-the-java part. As answered by #apangin, this set the ulimit of the shell process and the java process that spawned from this shell process, inherited this ulimit.

Related

How to open IE from a java program

Hey I tried to open IE from a java program.
the command start iexplorer works on command prompt and terminal but when used in a java program throws a IOException. When I execute the command cmd start iexplorer the program is just running without stopping for almost 15 minutes
String command = "cmd start iexplore";
try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
This is the call stack of from VScode:
image
can someone help me with this please
All you are doing is running "cmd" in background. If iexplore is on your system Path then this may work:
String[] cmd = new String[] {"cmd", "/c", "start iexplore"};
The "/c" option tells CMD.EXE to start the process, and then CMD exits immediately [in your case it is hanging around for more input]. Also you need to read the STDERR stream of the process to see any error messages from CMD.

Java ProcessBuilder is not able to start GNU screen

I'm experiencing a weird behaviour of java's ProcessBuilder.
What I try is to stop a running screen using a shell, delete a few folders and after that restart the screen using another shell script.
The first step, killing the running screen, runs perfectly using:
ProcessBuilder pb0 = new ProcessBuilder(System.getProperty("user.dir") + "/generator/stop.sh");
In this stop.sh shell I simply run
screen -X -S generator kill
which works as it should.
After that I delete my directorys using org.apache.commons.io.FileUtils and then I want to start the screen again. Currently I'm doing it like that:
System.out.println("Restarting the generator");
ProcessBuilder pb1 = new ProcessBuilder();
pb1.directory(new File(System.getProperty("user.dir") + "/generator"));
pb1.command("./start.sh");
try {
Process process = pb1.start();
System.out.printf("Started the generator with %d", process.waitFor());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
This gives out "Started the generator with 1" which indicates to me that the screen runs which is simply not the case when checking with screen -ls.
No errors, no clue on how to move forward from here
Inside start.sh:
screen -S generator java -Xms2G -Xmx2G -jar generator.jar
PS: I'm using Debian 10.
Maybe anyone can help me out here?
Greets!
You should never ignore process output, because it's buffer has limited length, and if you don't consume it, it will hang. Im not sure if this is causing your issue, but this is definitely something you should do.
It is also possible that process throws some error that you can't see because you are ignoring it's output (in which case, this will help you to investigate the issue).
Try this:
new ProcessBuilder()
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
...
This will redirect process output stream and input stream to it's parent (which is your application).

cmd.exe is hanging unexpectedly depending on where the file I use is located

This has got to be one of the strangest things I have ever observed. Consider the following Java program:
import java.io.IOException;
public class StrangeError {
public static void main(String[] args) {
try {
Process process = new ProcessBuilder(
"cmd",
"/c",
"\"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat\" amd64 && set"
).start();
process.waitFor();
} catch (IOException|InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
I compiled it with javac StrangeError.java, copied it to my server running Windows Server 2012 R2, and ran it with java StrangeError.
Here's where things start to get weird. The program hangs, waiting for the process it spawned to finish. This is not the expected behavior, since the vcvarsall.bat script should complete immediately as well as set.
So I started playing around and discovered the following:
Removing set causes vcvarsall.bat to terminate
Removing vcvarsall.bat causes set to terminate
Replacing && with || causes everything to terminate correctly
Copying vcvarsall.bat to a location on the desktop and changing the path causes everything to terminate correctly
A nearly equivalent program works fine in Go using the same commands
I get this output if I run everything in WinDbg and interrupt the process after it hangs
This does not appear to be reproducible with vcvarsall.bat from MSVC2013 but is also reproducible with MSVC2015 on Windows 10
What on earth is wrong with the original program? If I copy and paste the entire command (cmd /c "C:\...) into Start->Run, it immediately launches cmd and terminates, as expected.
Is this a bug with Java? Is this a bug with Windows?
Is this a bug with Java? Is this a bug with Windows?
It's a bug in your code. :-)
By default, a child process created using a ProcessBuilder object has output redirected to a pipe, the parent end of which can be obtained using Process.getInputStream() and which is not automatically drained if your code does not make use of it.
Since your code simply calls .waitFor without making any provision to drain the pipe, it will deadlock as soon as the pipe's buffer overflows. I believe the default buffer size is 4,096 bytes. On my machine, the output of the command you're running is 5,192 bytes, but this will vary depending on the original contents of the environment block. (From the sounds of it, the output length in your environment is borderline, only just above the limit, so that even small changes like changing the version of VS make a difference.)
One of the many possible solutions, depending on what you're actually trying to do, is to tell Java not to pipe the child's output:
import java.io.IOException;
public class StrangeError {
public static void main(String[] args) {
try {
ProcessBuilder processb = new ProcessBuilder(
"cmd",
"/c",
"\"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat\" amd64 && set"
);
processb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process process = processb.start();
process.waitFor();
} catch (IOException|InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
Not possible to read standard input and output error inside the same ProcessBuilder.
So you need to create two ProcessBuilder
Process process1 = new ProcessBuilder(
"cmd",
"/c",
"\"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat\",
"amd64");
Process process2 = new ProcessBuilder(
"cmd",
"/c",
"set");
process1.start();
if (process1.waitFor() == 0) {
process2.start();
if (process2.waitFor() == 0) {
// Successfull execution
}
}
And one thing : I don't think it is a good practice to do shell/batch launches with Java (or another language). Maybe you should use a script (shell, batch, python, perl...) to control standard input/output streams.

How to backup a mysql db from java using runtime/process classes?

I have a problem with this code:
try {
String cmd = "C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump.exe -uroot -proot foo_db -rC:\\backup.sql";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
} catch(Exception e) {}
It doesn't get any error, pratically it doesn't DO anything: no backup and no execution of the cmd. Where's the problem?
It's strange, because the cmd-text is correct... i've tried to do it with 'Execute' command in windows and it works, but in java no.
Thanks in advance.
Your first problem was, as #pcalcao pointed out, that you are not reporting the exception. You really should never do this. At the very least you should do:
} catch(Exception e) {
e.printStackTrace();
}
java.io.IOException: Cannot run program "C:\Program": CreateProcess error=193, %1 isn't a Win32 valid application
That says that you have a problem with your application path. By default, if exec() is called with a single argument, then it will break the arguments up by spaces. Since you have spaces in your path you need to pass an array of strings to exec(). Something like:
try {
String cmd =
"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump.exe";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(new String[] { cmd, "-uroot", "-proot", "foo_db",
"-rC:\\backup.sql" });
// wait for it to finish
proc.waitFor();
} catch(Exception e) {
e.printStackTrace();
}
The first argument in the string array passed to exec() is then the full path to the command -- this can have spaces. Each of the other array elements is an argument to the command.
Lastly, you will need to wait for the process to finish otherwise it will execute in the background. That's what the waitFor() does.
You need to escape the whitespace with \, exec is trying to execute "C:\Program", from what you've showed in your answer to my previous comment.
NEVER leave a catch clause empty.

unable to run ksh script from java: java.io.IOException: .: not found

I tried to run a shell script from java code, but I am facing problem. The script is in batchstart.sh file -
#!/bin/ksh
export DISPLAY=:0.0
Now the script is run with a dot on the command line -- . batchstart.sh
How do I run it from java? My java code is below. It throws the following exception -
java.io.IOException: .: not found
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:102)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:429)
at SetDisplay.main(SetDisplay.java:12)
import java.io.*;
public class SetDisplay {
public static void main(String[] args) {
File wd = new File("/myhomedir/");
System.out.println("Working Directory: " +wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec(". batchstart.sh", null, wd);
} catch (Exception e) {
e.printStackTrace();
}
}
}
How do I make the shell script run ?
I tried the following code as well, but that too doesn't work.
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 /home/");
out.println(". batchstart.sh");
out.println("exit");
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}
When run from the command line, using a dot at the start of a script indicates that the script should be run in the current environment, instead of spawning a new subshell and using a new copy of the current environment. This allows you to export a new value of an environment variable to be used by commands run later from the same interactive shell.
Obviously, this technique only works if you are running your batchstart.sh script from an actual shell. Java does not know how this mechanism works and so the dot means nothing to it. A script cannot modify the environment of the Java process it was called from.
If your goal is to change the value of the DISPLAY environment variable for other commands run by your Java process, consider using the ProcessBuilder class to specify a new environment for the child process. Java does not contain a built-in way to modify variables in its own environment.
The source command (".") is a shell built-in. You have to explicitly run /bin/ksh, passing your script name as the argument (followed by any script arguments).
You have a larger problem if you need to source the script. That usually means that environment changes happen in the context of the current shell, not a subshell.
This won't work with Java since Java's not a shell. You'll need to figure out how to change the environment with Java.
Of course, if I'm wrong and there's more to that script that just setting DISPLAY, it may work as suggested.
The method you're going to have to use depends on what you're trying to achieve(as in "Are you running other programs using exec() that rely on DISPLAY being set?" or "Does your Java program need DISPLAY to be set?").
If, as you state in your comment, it's only your Java program that needs DISPLAY set, just set it outside before your program runs. Create a cmd (or bash) file which sets the DISPLAY variable then calls the JRE to run your program.
#/bin/ksh
export DISPLAY-:0.0
/usr/bin/jre/java your_program blah.blah.blah
I would also modify your main() to check that it's set to something and exit gracefully if not:
if (System.getenv ("DISPLAY") == null)
// doesn't exist, exit gracefully.
The period "." is a shell built-in, and executes the script "in-place", analogous to #include in C/C++. Using "." outside of a shell-script has no meaning.
If you want to run the script from Java, you have to execute the script interpreter (/bin/ksh):
Runtime.getRuntime().exec("/bin/ksh batchstart.sh", ...)
but note that this is not semantically equivalent, since you're executing batchstart.sh as a sub-process instead of sourcing it.

Categories

Resources