Command stops whenever I log out of my server - java

I have a java program (text-based no GUI) that I have written and compiled and uploaded to a server.
I run it with java -cp myjar.jar mypackage.MyClass which starts it running processing a datafile with 20,000,000+ entries in it and printing output to System.out. I have calculated that it will take a very long time to process the data and I didn't want to have my laptop open for the 10 days of number crunching...
When I log out of my shell however, the process stops.
How can I execute the command and log out without it stopping? What is that even called?
I am using an Amazon Ubuntu EC2 server. I log in using a certificate from Mac OSX with terminal. The server seems to be using a bash shell.
Hope someone can help me out!
Jason.

Consider using screen instead of nohup. It allows you to create a virtual terminal that persists even after you logout/disconnect. When you reconnect to the server, you can immediately jump into the screen session you last had open.
Typical workflow on the server:
type screen (you may need to press space to leave intro page)
type in your command that you want to leave long-running (your java program, or an OS upgrade)
press ctrl a+d to leave screen (make sure to hold ctrl down)
To re-enter screen just use screen -r, and you will see the previous terminal and any running programs as you left it.

You can use nohup
nohup java -cp myjar.jar mypackage.MyClass > yourLogFile.log &
-----> http://ss64.com/bash/nohup.html

Related

Keeping terminal open when running a bash script from a desktop shortcut

I am running a bash script from my desktop off of a shortcut I made for it. When I click on it it says "Execute in Terminal" which is the option I want to do. The bash script compiles and runs a java program I made and the point of the script is to handle the errors that the program may through through the compiling prosses to someone who has very little programming experience (not really important to the question though). When I launch it, it will open the terminal as expected. When it encounters the error it will print out the error to the screen as one would expect it to do but then will immediately close the terminal it opened.
What I want is for the terminal to stay open until someone exits the terminal so they can read the errors.
I suggest you to add a line of code at the end of your bash script with a read operation as follows:
read -p 'Hit ENTER to exit'
This will keep the terminal visible until you hit ENTER.

Run shell script from Java to send data via USB

Exactly as specified in the title, I'm trying to send data to my ODROID-Show external screen via USB. I'm running a shell script that sends such data. The problem is I can simply run the command through Terminal and it runs successfully and data is sent to my little screen through USB port. When I try to run the same command via Java, Nothing happens.
Process proc = Runtime.getRuntime().exec("/bin/bash -c /home/ahmed/ODROID-SHOW-master/example/linux/images.sh /");
The specified command should have root privileges to run. That, I've switched to root then ran the code and nothing happened. Any thoughts how to solve such problem?
Edit:
IF you can show a code that executes given command prefixed by sudo this will absolutely work.
I was able to run the program as root. but, corrupted data are sent to ODROIDscreen rather than valid images. while it transfers successfully when ran through Terminal, Any thoughts why that happens?
I would check if the script executed by the bash interpreter requires certain environment variables set before execution.
I'd add a debug line in the executed shell script to dump the environment like "env > my_dump_env.txt" then run the script both from command line as well as from Java and do a diff see what is missing or is different.

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.

How to Run Commands through a already running Java?

I was wondering if it was possible to execute commands from PHP to a Java prompt which is already running?
I have tried the solution listed here:
How to run a shell command through PHP code?
and this provided no functionality
Let me explain
The java is running on one screen of the linux server
sudo apt-get install screen
and running the .jar file through the command line.
I am then running a webserver, which will have an admin accessibility to restricted areas, which will contain scrips to run specific commands through that already running .jar file?
You can implement some kind of IPC. The java file listens to a port and receives the commands. Or you can write the commands in a specific file which the java programm reads. I think under linux you can also use shared memory: http://www.php.net/manual/en/book.shmop.php
It is possible by sending the command to the screen session. I used this for a minecraft server once.
screen -S <sessionname> -X stuff "<command>\r"
This would (IIRC) provide the same output as if you where inside the screen, typed the command and pressed enter.
I hope this was what you wanted.

Categories

Resources