How to open terminal window and execute command in php? - java

I am making a website which requires a java program to run on the server (it is a ubuntu server). I want to achieve this by executing it from php. I tried 'shell_exec' and 'exec', but they both don't open a terminal window or execute the jar file. It did work on my windows pc, but I want to have it work on my linux server as well. I am using xampp as server.
The command I used that worked on windows:
shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS");

I fixed it myself by adding '2>&1 &' at the end of the shell_exec() command. This makes the php script wait untill the program is finished.
The new command:
shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS 2>&1 &");

Related

Execute shell script on remote windows server without any third party installation

I want to execute shell script on remote windows server through java code without any server setup and installations like SSH. My script file is already there on server. I just want to execute it on server cmd from my machine. Any possible solution?
According to definition of shell script from wiki:
"A shell script is a computer program designed to be run by the Unix shell"
Taking into consideration that you want to run a shell script on a windows server it will simply not work. I would suggest you to use some kind of environment as it is provided by cygwin or any other alternatives.
You can also check the following question.

How do I get rmi registry to work on windows command prompt?

So I need to work with some distributed systems on java RMI. I have already set a environment variable path to my JDK bin and so I can use javac and java commands on the command line.
However, when I try to set the rmi registry using rmiregistry, it doesn't work. It doesn't return "rmiregistry is not a recognised internal or external command" but it runs but it stays running for ages and I have to press ctrl+C to stop it from running as it won't stop itself.
The code I am entering into the command line is:
rmiregistry 8888 &
I've tried that on Linux before and it seems to work how I want it to but it's not working on Windows. What can I do to get RMI working on the windows command line?
The trailing & in a linux shell runs a program without waiting for it to terminate.
The windows equivalent is
start /B rmiregistry 8888
Just type the command below to start your rmiregistry. When it gets started, the rmiregistry.exe window will pop up on your screen.
start rmiregistry

How to get input from Putty to Java?

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.

How to open java program in other linux computer without terminal holding?

I have written a java program with jar file. The java program is to update status of linux server so it need to keep running, but the linux server is in data center, so I need to remote to server to open the program. I use ssh to login linux server. Use command of "java -jar file.jar" to run the program.
However, the java program of the linux server will close if I close the terminal in my computer. Since I cannot keep opening my computer, I wanna know how to open the java programming without holding my computer terminal.
you need to use nohup to keep the program running after you log out.:
server:~name$> nohup java -jar file.jar &
this will keep your program running
Two ways
One
nohup java -jar file.jar &
Another
java -jar file.jar &
In both cases your process will go in background however the process will terminate in the second approach when shell terminates in second case.
If this program is intended to be running on all your machines for monitoring purposes, you should be running it as a service from your server's init system (systemd for most systems these days). You can use the Java Service Wrapper or jsvc or write your own init script.
Another solution apart from the proposed one:
screen -d -m java -jar your.jar
You will then have a detached screen with your java command in it. List with screen -l, reattach with screen -D -RR <screenid_obtained_via_screen_-ls>

Executing .bat files using jenkins

I'm trying to run a jar file using a bat command with jenkins. and I want to popup the cmd and execute the jar file. but the problem is jenkins execute commands inside its console. Then i inserted "start" command and hoped it'll work coz it creates a separate cmd to run the jar.
here is my bat code
start "window_name" java -jar myjarfile.jar
but when i executing using it jenkins it doesn't create a separate cmd window but it executes the jar file anyhow. it shows this line,
C:\Update>start "window_name" java -jar myjarfile.jar
any idea how to solve this? I want to pop up a black window when executing.
The trick is figuring out in what session you want to start the cmd.exe. On a remote server (which is most often the case with Jenkins), it's not necessary straight forward. Your Remote Desktop Session is not in the same session as someone who is logged in physically at the console.
Bring up Windows Task Manager
Click the Users tab
Note down the ID of the session of the logged in user that you want
Download psexec from Windows Sysinternals
Edited from here downwards
Open elevated command prompt: type cmd into Start's quicksearch, right click cmd.exe, select Run as Administrator.
Type C:\path\to\psexec.exe -accepteula and press enter.
Type C:\path\to\psexec.exe -i 1 cmd and press enter. (If you see a command prompt appear, all is good, close it now)
In Job configuration, configure Execute Windows Batch command step
Write the following:
C:\path\to\psexec.exe -accepteula && C:\path\to\psexec.exe -i 1 cmd /c start C:\full\path\to\java.exe -jar myjarfile.jar
A more detailed explanation is provided in this answer Open Excel on Jenkins CI
Thanks Guys, May be your solutions too will do the trick. Finally what I did is i created a socket program and executed server myself. Then scheduled jenkins to execute the client.(Server in my environment & client in jenkin's environment) When client connects to the server it executes the bat file. Now everything works fine.

Categories

Resources