Pass a command to program running in detachtty - java

I've got a Java command-line application that I start with detachtty /var/run/handle /path/to/script. I can bring it to the foreground by running attachtty /var/run/handle. When active, the application displays a log of things it has printed out, and a line to input commands. Is there a way I could write a shell script that would send it a command, wait a short while, and then echo out whatever new characters attachtty would have printed to the screen?

Related

logging with subprocess in Python

I am launching a Java command through subprocess in Python.
JAVA_CMD = ['java', '-Xmx10200m', '-cp', '/path/to/class', '-Dlog4j.configurationFile=/path/to/logfile']
filename,k,mail = '/path/to/file2',"20", 'help#so.com'
subprocess.Popen(JAVA_CMD + [filename,K,mail], stdout=subprocess.PIPE)
## rest of the code
Above command runs for 24-48 hours.
Does anyone know if the logging in java command will work with above command? Currently I am able to launch the java command without having to wait for the response but, logging is not working. It is not creating any log files.
Also, is there an automatic timeout related to above process? One of the commands is dying again and again. It might be a problem in the code but I was wondering if subprocess has a timeout related to it which is killing the process.
Ideally what I want is the ability to launch a java command from within Python as if the command was launched by the user and the process should keep on running indefinitely, till it is completed. Python should be able to work on further without having to worry about the java command launched.

Java Process running from Windows Powershell sometimes randomly stops until pressing Ctr+C

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.

Java manage a process that doesn't exit automatically

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?

How to view a runnable .jar's output via SSH client

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.

About program running independent from command line under unix

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.

Categories

Resources