missing command output

For some Reason the output that i get from executing a command via Runtime.getRuntime().exec("command") is incomplete. In this case I only get "Reading packet List... " isntead of also getting things likke "All packets are up to date." etc even though i get those when executing it manually in sheel. It ried stuff with debugger but wasnt able to find a work around for the problemm. Environment Linux Debian Server
// Run apt update command
Process updateProcess = Runtime.getRuntime().exec("apt update");
updateProcess.waitFor();
// Read the output of apt update
BufferedReader updateReader = new BufferedReader(new InputStreamReader(updateProcess.getInputStream()));
String updateLine;
boolean upToDate = false;

// Read all the lines of output
StringBuilder updateOutput = new StringBuilder();
while ((updateLine = updateReader.readLine()) != null) {
updateOutput.append(updateLine).append("\n");
System.out.println("Proccess Reader: " + updateLine);
}

// Check if all packages are up to date
if (updateOutput.toString().contains("All packages are up to date.")) {
upToDate = true;
}
// Run apt update command
Process updateProcess = Runtime.getRuntime().exec("apt update");
updateProcess.waitFor();
// Read the output of apt update
BufferedReader updateReader = new BufferedReader(new InputStreamReader(updateProcess.getInputStream()));
String updateLine;
boolean upToDate = false;

// Read all the lines of output
StringBuilder updateOutput = new StringBuilder();
while ((updateLine = updateReader.readLine()) != null) {
updateOutput.append(updateLine).append("\n");
System.out.println("Proccess Reader: " + updateLine);
}

// Check if all packages are up to date
if (updateOutput.toString().contains("All packages are up to date.")) {
upToDate = true;
}
I am am aware that running java as root would be a great security risk. I circumvented it by using a dummy user which does not have sudo permission and allowing him to execute apt update and apt list --upgradeable.
30 Replies
JavaBot
JavaBot10mo ago
This post has been reserved for your question.
Hey @Hype_the_Time! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Peter Rader
Peter Rader10mo ago
Well, I am pretty sure that the process write an error message. This error-message does not find its way to the inputstream. Try to print whats inside the getErrorStream() aswell. I believe the subprocess does not have a shell and you have to set the full path to the apt-executable.
Hype_the_Time
Hype_the_TimeOP10mo ago
ok ill look
Peter Rader
Peter Rader10mo ago
Oh, hm. Since the "Reading packet List..." print to the stream, apt-command might found successfully. My bad Anyway, check the getErrorStream(). If you like, apache has a stunning abstraction. "commons-exec", I use it very often.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.4.0</version>
</dependency>
Hype_the_Time
Hype_the_TimeOP10mo ago
no it was an error thought i cheked that gues i didnt. whats that?
Peter Rader
Peter Rader10mo ago
That code is a Maven dependency. Do you use maven?
Hype_the_Time
Hype_the_TimeOP10mo ago
i mean yes but whats cool about the packet
Peter Rader
Peter Rader10mo ago
You have a watchdog for processes, you can answer questions of the app like "are you sure?[y/n]". It has a community.
dan1st
dan1st10mo ago
I mean, for these questions, one can use Scanner/BufferedReader
Peter Rader
Peter Rader10mo ago
Yes, that true. It just made my personal life much easier.
Hype_the_Time
Hype_the_TimeOP10mo ago
i mean is there a good way to detect a blue screen during package installation (prompts during installation after apt upgrade which you can overwrite using -y paramter)
Peter Rader
Peter Rader10mo ago
🤨 You need to detect a blue-screen-of-death?
Hype_the_Time
Hype_the_TimeOP10mo ago
sometimes while its rare its not enver the case you can get a prompt like hey do you want to install the maintaniers version or keep your own because you modified it. during apt upgrade process
Peter Rader
Peter Rader10mo ago
Ah, ok, a blue dialog (I think it is called OWL).
Hype_the_Time
Hype_the_TimeOP10mo ago
and because im not sitting in fornt of it that thing probably is holding it and i cant answer not see in this case maning i would need to kill it which means i need to do an reconfigure aftewards in best case no idea about how the proper name is for that.
dan1st
dan1st10mo ago
Linux doesn't have BSODs technically nothing blue there but kernel panics are an option Anyways, If that happens, your application won't be running any more
Hype_the_Time
Hype_the_TimeOP10mo ago
the blue in case of the installation screen of bullsyeye is similar. not the graphical version though.
dan1st
dan1st10mo ago
well if the kernel has issues, your application won't be running
Peter Rader
Peter Rader10mo ago
No, he means a dialog in the console.
dan1st
dan1st10mo ago
oh that configuration thing
Hype_the_Time
Hype_the_TimeOP10mo ago
when updating for example with ssh and you said like hey only ssh keys you modified it and if there are new fields added etc it may ask hey you modfied it do you want to update ir or keep your own etc i need a way to handle THAT
dan1st
dan1st10mo ago
Ask Ubuntu
How can I install apt packages non-interactively?
If I run: sudo apt-get --yes install postfix or sudo bash -c 'yes | apt-get --yes install postfix' an interactive prompt appears to configure postfix. I need to automate the installation of po...
Hype_the_Time
Hype_the_TimeOP10mo ago
hm yeah looks good. would still be good to know like hye you got a question and we answered it. in case something goes wrong
Peter Rader
Peter Rader10mo ago
Hm in this case there might be another approach. Start programatically a ssh-connection, you controll the full screen AND it might solve your user-rights-problem.
Hype_the_Time
Hype_the_TimeOP10mo ago
user rights is already done. i just have a application manager where i want to be hey theres an update click to install and reboot after or just install so i see when there are updates quickly
Hype_the_Time
Hype_the_TimeOP10mo ago
something in this direction. but name and id are incorrect and i need to have a look at the list but in concept
No description
dan1st
dan1st10mo ago
name and id?
Hype_the_Time
Hype_the_TimeOP10mo ago
reversed, name is what is displayed id is what discord tells you as button id. i put them in wrong order at definition
dan1st
dan1st10mo ago
I know about the Discord API So what's the problem?
Hype_the_Time
Hype_the_TimeOP10mo ago
oh nothing setting it up atm matter of time
Want results from more Discord servers?
Add your server