I've a no gui program similar at su that works like the figure and I want to manage it with Java Runtime to read its exit code.
To read its exit code I would like close the program soon as possible but I can't wait for process finish because if the machine is supported the process doesn't finish while I send an "exit" command. And I can't send an "exit" string because I don't know how many seconds it works and I don't know if the machine is supported.
If I launch the program in terminal, I can see that while it "works some seconds" I've only the still cursor, if the machine is supported it enters in a sub-shell and change the shell header otherwise it exit and the shell header doesn't change.
Can I read these 3 steps in java? Which is the best way to manage a process like this?
Related
I'm using JSch to automate remotely launching a python script from a background Java process.
Modifying the Shell.java example included in the JSch package, I've successfully installed JSch, connected to my Pi, and even commented out the user/domain/password/host key checking prompt boxes in favor of storing these values directly in my Java code.
After my java code logs into the remote Pi, I'd like it to send something like
sudo nohup python2 myFoo.py & disown
to the terminal.
In the Shell.java example I'm modifying, I see lines of code redirecting the input and output streams of the channel object to System.in and System.out but I'd like to simply manually inject that above line into the remote terminal and disconnect.
Why/my goal:
I have a small mesh network of Pi's running a script for most of the day.
I'd like to eliminate downtime, but the code sometimes stops working after looping for 3-4 days straight, (sometimes as long as a week straight before the code bugs out and stops).
The script running on each node updates a mySQL database with a "last check in" field.
I'm hoping to write a small background program in Java that will run indefinitely on my server, checking the "last check in" for each station every now and then, and if it notices a node go down, remotely ssh into it and sudo reboot now, wait about 60-100 seconds, then sudo nohup python2 myFoo.py & disown
You have picked a wrong example. The "shell" channel is for implementing an interactive shell session, not for automating a command execution.
Use the "exec" channel, see the Exec.java example.
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.connect();
...
For a full code, see How to read JSch command output? – Which shows how to correctly read both standard and error output simultaneously, to allow command to complete and to collect all output including the errors.
I am running a Java process with several threads from a Powershell on Windows Server in Admin Mode.
Sometimes it randomly freezes until I press Ctrl+C, then it just picks up the work again.
Has anyone ever come across this problem and point me to where to look at?
If you start the process with Runtime.exec(..), it is sometimes necessary to read Bytes from the Process.getInputStream() resp. Process.getErrorStream(), else the process blocks, when it tries to write more to std-out (or std-err) than a certain buffer size.
I had this problem often, when starting Shell scripts.
You can create a Background Thread to read periodically from these streams.
Alternatively and more easy, you can use the ProcessBuilder class to start the shell process and use "inheritIO()" method.
I have a .jar runnable running on my server. When I run the file locally I am able to see its output via my IDE. Similarly I can connect via SSH and run the file and see the output, but when I close the session the JAR exits.
Is there any way to have my application continuously running and then tapping into the java applications output using a terminal service like SSH without having to stop/start the application.
Any help would be appreciated.
You can use either screen or nohup
What is happening is that when you close your SSH session, there is a hangup call made to the program.
You could use nohup. Nohup stands for "no hangup"
nohup java -jar myJar.jar > outputFile.txt
That would run the program in the backround and send all output to outputFile.txt. When you end your session it will continue running. You must use a kill command to kill it.
The second option is you could use screen which essentually creates a detachable "ghost session". When you run screen it looks like you are in a different ssh session. Then when you detach from screen, the processes continues in the backround. When you exit your ssh session, screen continues to run.
You simply ssh back into the server, and re-attach to your screen session, and like magic, your program is still running with all relevent output. A simple read on the man page should catch you up on how to do this.
man screen
Lastly, I decided to add this third option, not because its viable, but simply so you can understand that it is an option. (Some people would claim this is the only REAL option as it is what your SUPPOSED to do. Frankly I couldn't care less and think you should do whatever is the easiest to get to your goal.)
You can also edit your program to swallow the hangup signal. The program would then always run in the backround. You can then use
java -jar myJar.jar & > outputFile.com
The & tells the command to start a new process (aka run in the backround) and the > sends the output to a file. This is how normal server applications like tomcat or spring boot work. They simply swallow the hangup call.
I am writing a java program that runs under unix.
It would like run forever. But when I start it from command line, I have to leave that window open always until the program stop.
Could anyone give me some idea about how can i run it at back end? Just start it from command line then I could close that command line.
Thank you very much.
If you don't want to "daemonize" it you can just use nohup:
$ nohup your-program &
$ exit
and your-program will continue to run in the background until it finishes.
You're asking about making your program a "daemon". Check out these links about daemonizing java programs, and this one about daemonizing any process in linux.
...Another option is to use the "screen" utility. Its a little tricky if you've never used it, but you can do things like launch a job in a terminal at work and easily reconnect to the same terminal from anywhere else to check on the status of the job. I use it for connecting to servers where I run long-running jobs. Without using screen my process would die if my local machine crashes, or the power goes out, or fire, etc.
I have a Java application launched via Webstart. About half the time, this program will not die. This program listens on a socket and when it receives a particular command, should close down. When it receives this command, it prints out what it normally should to the java console (that it received the shutdown command), the Java console closes, all the windows close, but the java process continues to run.
I have tried switching System.exit(0) to Runtime.getRuntime().halt(0) with no success - the same problem happens (my initial thought was a shutdown hook was affecting it).
Furthermore, when I have VisualVM connected to the Java process, it also indicates that it loses the connection when halt or exit are called, and I cannot reconnect, but the java process continues to exist.
Any ideas on how I could solve this? I've tries wrapping the program in try { } and exit/halt in finally { } to ensure nothing was holding it up, tried halting instead of exiting, etc...
My only thought is Java could be retaining a system resource (file handle to socket or something) or be stuck in a system call so the system won't let it quit - but the Java console closes, which to me seems like an indication the VM is trying to shutdown...
Any thoughts or ideas are appreciated!