Java Linux Daemons/Services with Server Side Input - java

I have a java executable that runs a service. It allows the admin to input commands while the service is running. It usually requires an open terminal to run. I connect to my server via ssh.
I can launch the service and it works but I can't exit the terminal without the service closing.
I also can't create a simple daemon because then I won't have access to give the service server-side input.
Is there any sort of daemon where I can have a terminal interface for input, or is there a persistent ssh terminal where even when I exit it will be left running?

to make your process do not close when you exit the terminal you can use command like:
nohup <command/run> >/path/to/log 2>/path/to/errlog &
If you want persistent terminal you can use command like screen or tmux

Related

how to access remote Linux machine through Java API

I need to access and run Linux commands on a remote CentOS machine through Java code. Please suggest me any API to access run the commands and also I need to get the output of the commands to be printed on the Java console.
Check out JSch - it allows you to connect via SSH, execute commands remotely and transfer files.
You can use Java ProcessorBuilder and Process classes to start an ssh process that executes remote command i.e. start an ssh process (e.g. ssh username#REMOTE_MACHINE 'CMD_ON_REMOTE_MACHINE') and read the output of the executed command using getInputStream() method of Process class.

Maintaining a SSH channel in Java with JSCH

I want to control a remote system in Java via SSH using JSCH.
The front end is a simple button GUI which triggers the execution of a command.
Some of the controls are time critical, there should be no big delay between button press and command execution.
My problem:
Every time a new channel is opened, the back-end needs about 8 seconds to initialize until the command is executed. (The back-end interface is implemented with RBSH afaik)
If I run a normal session via a console client, everything runs fine without bigger delays.
My question:
Is there a way to initialize a channel to execute some commands and read the output(and only the command output) back sequentially?
I already figured out that session.openChannel("shell") could give the desired functionality, but I cant figure out how to do that properly.
EDIT: I'm not tied to JSCH. If there's another library which can do that, I'm also open for that
You want an "exec" channel rather than a "shell" channel. SCP uses an exec channel, so look at one of the SCP examples or one of the SCP libraries on the Internet.
Alternately, if you control the remote server, you could define a "subsystem" for the command that you want to run, and run it through a subsystem channel. The big difference between an exec channel and a subsystem is who specifies the command to be executed. An exec channel will execute a command provided by the client. With a subsystem, the client just requests the subsystem by name, and the server runs the correct command (or provides the service in some other way). SFTP uses a subsystem called "sftp-server", so you could look at how Jsch's SFTP classes are implemented.

Operating terminal session from Java

I am trying to make a terminal emulator in Java. The java program will accept the commands from user, and show its output to them. I can emulate simple commands like 'ls', but I don't know how to handle commands like 'cd'. This is because, I am using exec() method for executing terminal commands. So, all the commands are executed at current directory. The commands like 'cd ..' are executed, but then they have no persistent effect, because each command is separately executed by exec().
Any Ideas How I can emulate a whole session??
If you are executing commands with exec(), you are not writing a terminal emulator; you are writing a shell. In that case, you will need to keep track of things the shell keeps track of, like environment variables and working directory.
If you really want to write a terminal emulator, you would be talking to a shell process through a pseudo-terminal. Then your program would just be keeping track of the things a terminal keeps track of, like the line state and what appears on the screen.
Working with a pseudo-terminal from Java will be a little tricky, because most of the documentation assumes you are using a C api. man pty should get you started. Your Java process will have to open the master side of the pseudo-terminal with FileStream objects. I'm not sure there is a way within Java to get a child process to open the slave side of the pseudo-terminal; you might have to invoke a shell command with exec() that starts another shell command with standard input/output/error redirected to the slave side of the pseudo terminal.
JSch is a pure Java implementation of SSH2.
JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.
http://www.jcraft.com/jsch/
You should really give a try to Ganymed.
Ganymed SSH-2 for Java is a library which implements the SSH-2
protocol in pure Java (tested on J2SE 1.4.2 and 5.0). It allows one to
connect to SSH servers from within Java programs. It supports SSH
sessions (remote command execution and shell access), local and remote
port forwarding, local stream forwarding, X11 forwarding, SCP and
SFTP.
http://www.ganymed.ethz.ch/ssh2/
Ganymed along with apache FTP client you can also download and upload files.
Also there is a inbuilt example code for terminal emulation in Ganymed.
The following is a link to a project which is did using Ganymed along with apache FTP client.
GITHUB
Happy Coding!!

how to stop a program running in the server?

I m doing a eclipse plugin project to create an IDE for a particular language.
For running i connect to the server and ask the user the command,type of connection...
After the program has started execution The only way to stop the execution of the program is by pressing "ctrl+C" when done in the command prompt.
I run the program by sending the server the following command:
"probevue filename.e >output.txt"
when i give this command it is running,but i m not able to stop the program...
i.e when i press Ctrl+C the program should stop execution.
How shall i do this?
Thanks in Advance.
after reading into this article about probevue it seems that you have started a dynamic session. i think the program you're calling is never terminating itself.
http://pic.dhe.ibm.com/infocenter/aix/v7r1/index.jsp?topic=%2Fcom.ibm.aix.cmds%2Fdoc%2Faixcmds4%2Fprobevue.htm
you should first read how to invoke a command properly. e.g. -> http://www.javalobby.org/java/forums/t53333.html
then you should think about how would you terminate the command if you wouldn't have the ctrl+c option, like finding out the process-id of the shell command you executed before (e.g.: ps -ef | grep ) and terminating it by using another shell command (e.g.: kill -9 )
hope this helps you find a solution.
Common eclipse server plugins like JBoss Tools call the server scripts that are delivered with the server for starting and stopping. Deployment/undeployment and starting/stopping of applications is done via the management port.
So either you have a manageable server to control your program, or you could work around the problem by writing the scripts yourself.

How to fire Java method using bash

Suppose I launch a Java application:
java -cp whatever.jar com.example.Start
Process launches ok and keeps running with PID 1314.
Now I would like the system to fire a method by users request.
How can I use bash to signal the running PID and have it fire a method?
My thought is to have bash echo data to the Java processes via a named pipe, which I'm pretty sure Java has support for.
To communicate with a Java process, you would normally use RMI from another process (this could be in the same JAR)
However, if you want a pure bash/unix utilities solution, you could have the application listen on a port for commands and send back responses. This means you could use plain telnet to send commands and get output. One example of this is to use a http server with wget or you could have a simple socket based solution.

Categories