Hello I am making a Java program in an Ubuntu Web Server but it is always supposed to be running in an infinite loop, or at least until I stop it. When I run it in the Ubuntu console it won't allow me to keep using the console. To work around this I have been using the "screen" command and detaching the screen.
I was wondering if there is a better way of doing this without working with the screen command?
Run a program immune to hangups
If you're happy with starting the Java program up manually from the command line, but just want to not have screen running, you can use the "nohup" command to start the Java process in such as way that the Java program will continue running even if you close the console window or log out.
$ nohup java ...
nohup: ignoring input and appending output to `nohup.out'
$
Run a program in the background
If you don't mind the Java program stopping if you close the console window or log out, you can skip using "nohup" and only append a "&" to the end of the command to tell your shell to run the application in the background. You may also want to add " > program.log 2>&1" to the command to avoid having any output from the program show up in the console window while you're using it for other purposes.
$ java ... > program.log 2>&1 &
[2] 3128
$ jobs
[2]+ Running java ... > program.log 2>&1 &
$
Run a program as a daemon
If you want the Java program to automatically start up every time you reboot the machine, you should look into creating a SYSV init-script, or as you're running on Ubuntu, an Upstart Job definiton.
Related
I'm trying to run a java springboot server using the following nohup command.
nohup java -jar webservices-0.0.1-SNAPSHOT.jar > nohup.out 2>&1 &
But after running this, I'm getting the following output & I have to hit enter to run the subsequent commands.
$ + nohup java -jar -Dspring.config.additional-location=config/application-dev.properties webservices-0.0.1-SNAPSHOT.jar
Is there anyway to avoid this in bash?
It looks like you have shell debug tracing (set -x mode) turned on, meaning bash will print each command before executing it (but after expanding variables etc). Since the nohup java ... command is backgrounded, this happens asynchronously, and happens to get printed after the shell has printed its prompt, so you get "$ " (your shell prompt) followed by "+ nohup java ... (the debug trace).
(Note: you have errors & output from the command redirected to nohup.out, but since the trace is printed by the shell, not the command itself, the redirect doesn't apply.)
You don't actually need to press return at this point; the only thing pressing return does is get you a new, clean (not mixed with debug tracing) prompt. You could just enter another command as normal. However, since the shell is confused about where on the line you are, features like command editing and history recall may not work properly.
If you want to avoid this, I think your main options are to either turn off debug tracing (set +x) before running the command, or add a delay before the shell prints its next prompt:
nohup java -jar webservices-0.0.1-SNAPSHOT.jar > nohup.out 2>&1 & sleep 1
Here, the nohup java ... command runs in the background as usual, but the sleep 1 command runs in the foreground and so the shell won't print its next prompt for a second (which should be enough time for the background process to print its debug trace).
Can someone explain to me why this is the case. Let's say I have a simple java program that loops forever until I kill the process.
If I make a shell script like this.
#!/bin/bash
# clears the terminal
clear
# runs the java executable
java -jar java_program.jar
# prevents the terminal from closing
$SHELL
The shell script runs the program and if I use hotkey CTRL + C it ends the java process exactly how I want it. But when I use the "X" in the top right corner the java process does not end. Why does it do this how do I solve this issue?
I know the java process does not end because if I look in task manager the process is still running.
I am using windows 10.
I do not have git bash installed, but the behaviour you perceive comes from the terminal emulator that is used around git bash. It is not the windows native terminal emulator (and running cmd), it is not the cygwin terminal emuator.
Since you have it installed figure out the software emulating the terminal and see how that treats the processes when you close the terminal window. Maybe it is configurable.
I am running simple java program using on redhat 6.2
I am starting the program using the following command
java -cp "$CLASSPATH:/home/vikas/Myjar.jar" com.vikas.MyProgram <args>
The program has an infinite while loop and should always be running.
However the problem is when i do the following on another terminal on same machine
ps -ef| grep -i MyProgram
This does not produce any result. However, I can see my program is running fine.
However, if I execute
ps -ef|grep -i java
This fetches multiple results including one running as my user "vikas"
Is there a way i can force the program to run by its own name?
I'm trying to code a shell script to start/stop torrents using vuze's console UI through SSH:
https://wiki.vuze.com/w/Console_UI
I've downloaded vuze and everything works fine until I type this command:
java -jar Azureus2.jar --ui=console
After that, no command in my script works unless I quit that console.
Any solutions please? And if it's not feasible using shell scripts, any suggestions please?
Thanks.
Basically, the moment you run that command, your java program runs 'in the foreground', which means the rest of your script stops executing until your program exits.
If you want to keep on running the rest of your script while your java program executes you have to run your program in the background. One way to do that is as #Alp suggests:
java -jar Azureus2.jar --ui=console &
I have a Bash script that goes something like this
#!/bin/bash
for i in $(seq 1 100); do
nohup java -jar myProgram.jar -myParameter1 input$i -myParameter2 $i > /dev/null 2>&1 &
done
The Java program (myProgram.jar) prints a lot of output to the stderr and stdout that I don't need and also takes at least 10 minutes to run, so I want to be able to log off from the remote computer and have it keep going for hours (thus, I use nohup and redirect stdout and stderr to /dev/null with 2>&1).
However, if I run this script as it is, it will just keep running the programs one on top of the other until it runs out of memory/processors (I suppose) and then it runs the next program once one of them finishes. I wouldn't mind this happening but it is a shared server so I cannot slow it down for several hours. Is there a way to prevent Bash from going onto the next loop iteration until the current myProgram is finished running?
Remove & from end of your command line to make it run in foreground. With & at end of line you are forcing your program to run in background in the loop.