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.
Related
Is it possible to create for only one process two different CLI (CommandLineInterface)?
I would like to have one CLI with my real program, and another CLI for a chat, so that i can send command to my program and send messages in the chat at the same time, and obviously have different views for the program and the chat.
(edit)
the program is a game and the chat is to make communication between all player, but when i start my program in eclipse, that program strat with only a console and here i would like to have two console in one there is the game with its action and state and in the other one i would like to have all the messages in the chat.
I know that i can build another process from zero and integrate it with process builder, but i would like to have all in one process.
If I assume that by CLI you mean the main terminal from where you execute your program, the answer is NO, regardless the OS.
There are couple of options to implement additional CLI interfaces in the same process:
listening on socket and waiting for client(s) to connect by e.g. telnet
opening a window that implements the CLI
under UNIX you can spawn e.g. a xterm and process its IO in your process
Under Linux or OSX, just open a new terminal window, and you will have an additional CLI to work with, and yes, you can try your program from those two different environments simultaneously and independently.
Under Windows, I couldn't say. You're probably using cygwin or something like that, so you should probably try to be a bit more specific in your question to get more attention.
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.
I want to write a program which connects to remote machines via ssh and install predefined software. Also I wanna make process of installing clear for users by make all it visible to users. I have faced some problems: how to open terminal from java and send commands to it?(OutputStream doesn't work) How to execute command in this terminal when I already ssh? I want to run local scripts on the remote machine and allow user? to interact with terminal, when script is running (for example accept licence of software and so on).
I was trying something like this but it is not working.
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("x-terminal-emulator -e ./script.sh");
In theory it is possible. You'll need a java library, such as JSch, to interact directly with a terminal via ssh, and then make use of the screen utility to share a single terminal screen between your java prog and a user.
From your java prog:
screen -d -m -S shared
screen -rx shared
<type your installation commands>
From the remote user:
screen -rx shared
Of course the remote user must wait until the java prog initializes the screen in order to attach to it.
Please note that all kinds of things can go wrong when you let users interact with the screen. Your program must be smart enough to handle it.
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.