I have a java app from which I run console based programs on linux system, I am reading the output of those programs and then my java app is sending it to a webpage.
But once I close my java app all the processes will get "stuck" or they just simply crash. So everytime I want to make some changes to my java app and I need to restart it I also have to close all processes that were running from my app. I would like to save their PIDs when closing my app and then take control (output streams) over those processes again based on saved PIDs of the processes.
Is there any way to do it?
I am running my programs like this:
ProcessBuilder processBuilder = new ProcessBuilder(new String[] { "su", "-
s", "/bin/sh", "myuser", "-c", "java -jar myjar.jar" });,
Process p = processBuilder.start();
Edit:
My problem is not finding the process PID my problem is that my subprocesses lanched from my java app are crashing after my java app is closed/terminated and I need them to continue running even while my app is restarting/stopped.
Your problem is due to what is called Unix job control.
Like many shells do, /bin/sh intercepts SIGHUP and SIGINT signals, and before exiting, it sends signals to some of its child processes groups, depending on its configuration and on their state (for instance, stopped background processes receive a SIGCONT).
So, when your main java app is closed, the /bin/sh shell that your app had forked is terminated, and just before exiting, it sends a SIGHUP signal to its subprocesses corresponding to the command java -jar myjar.jar.
So, the answer to your question is: just use the huponexit /bin/sh shell option to avoid killing subprocesses. They will be detached from the controlling terminal, if any, but they will not be killed.
So, replace this java -jar myjar.jar by shopt -u huponexit; java -jar myjar.jar:
ProcessBuilder processBuilder =
new ProcessBuilder(new String[] {
"su", "-s", "/bin/sh", "myuser", "-c",
"shopt -u huponexit; java -jar myjar.jar"
});
Process p = processBuilder.start();
Try to run your command like this:
Runtime.getRuntime().exec("gedit");
It executes the specified string command in a separate process.
Some time ago I have found some useful information here. Try this.
I would use the ps command in Linux to get the number of each process(s) running which you want to control, of course you would execute it just as you have above with your ProcessBuilder. I would then pipe; "|" (Linux Command), the output into a file you have saved somewhere in your Java project.
The Linux command to execute from your Java program would look something a long the lines of
ps -A | grep "your_program_name" > /path/to/your/project/my_process_list_file.txt
Where the > stores the output of the command executed to your file.
I would then read from this file and execute some other Linux commands to take control of that process in whichever way you desire.
Good Luck, and happy coding my friend!
I have a piece of Java code in which I am using Java Processbuilder to run powershell command on a remote machine. The remote machine is Windows but can I run this from Mac? I don't have Mac so I can't test it but users might have.
String[] commands = new String[] {"powershell.exe", "invoke-command", "-computerName", "myCom"};
new ProcessBuilder(commands);
Thanks
It won't work. I was thinking I can invoke the powershell somehow to run on the remote machine rather than the client itself. But I didn't find a way to do so . So had to use that code for Windows and write a separate one for Linux.
I am creating a batch file from Java which is deployed in a Linux machine and moved from Linux to Windows using Samba.
We want the batch file to be triggered from Java. Can you please highlight some steps?
I cannot use below command as it will use Linux run time:
Runtime.getRuntime().exec("cmd /c start buildFile.bat");
Any suggestions?
You can execute processes from Java on Linux as described here. The process is executed from the operationg system and depends on the operating system. You could rewrite your buildFile.bat as a linux shell script on execute it from you Java program.
I created Batch file in Windows using Samba from Linux machine and let windows scheduler handle running batch file.
Problem solved :)
I am quite new to Ubuntu and shell programming. I am using putty to execute vmstat and iostat commands on Ubuntu. I need to execute those commands in java (Windows environment) and get an InputStream of the result. Is it possible?
Yes. Use plink (a command-line interface to the PuTTY back end).
For example,
plink remote_host "ls -l"
I've successfully used this to, from a Windows-based TFS Build, execute an Android build on a Linux box. So it's possible to work with the output from plink programatically.
Here's an example of how to call a shell command from within Java.
I'm trying to run batch scripts from linux on a windows machine
I heard the nmap has a solution but I can't get an example
Is it possible? How?
Noam
Nmap (a network scanning tool) is not for running Unix/Linux scripts on Windows. On Linux, the usual shell is bash. If you want to run bash scripts on Windows, you might use cygwin, you might try Windows Services for Unix or even win-bash.