I have a jar that runs forever (infinite loop with socket listening thread) and need it to run in the background at all times. An example would be: "java -jar test.jar" How do I do this? Thanks in advance!
appending & at the back. However, using this, the program will still be terminated if you closed the terminal that started the program.
Start a screen session, and start the program inside it; you can detach the screen session and close the terminal. Later on, you can attach to the session again and found yourself back on the console as if you've been there all along. However, you will need to start a screen session before running the program, and if you forgot to do that, you can't do anything about it.
Use disown job control from your shell. This will detach the task from your tty and your program won't be terminated when the tty is closed. However, I don't think there's any way to reattach a disowned job.
If by "at all times", you mean it gets started when the machine boots, you'll need to start it as part of the boot-time scripts. The dirty way to do this is to add
java -jar test.jar >/dev/null 2>&1 &
to /etc/rc.local (or replace /dev/null with your favorite log file).
Better would be to make an init.d script that gets started at the appropriate runlevels (you might not want it on runlevel 0, 1 or 6, for instance). That way, you can also have it be restarted automatically if it dies and have a nicer interface to temporarily stopping it.
A command line prompt I have always used for long run times to last through logoffs is "nohup" so in your case is
The & is important so you can get another shell running. I believe this will not last through reboots.
several ways:
appending
&
at the back. However, using this, the program will still be terminated if you closed the terminal that started the program.Start a
screen
session, and start the program inside it; you can detach thescreen
session and close the terminal. Later on, you can attach to the session again and found yourself back on the console as if you've been there all along. However, you will need to start a screen session before running the program, and if you forgot to do that, you can't do anything about it.Use
disown
job control from your shell. This will detach the task from your tty and your program won't be terminated when the tty is closed. However, I don't think there's any way to reattach a disowned job.If by "at all times", you mean it gets started when the machine boots, you'll need to start it as part of the boot-time scripts. The dirty way to do this is to add
java -jar test.jar >/dev/null 2>&1 &
to
/etc/rc.local
(or replace/dev/null
with your favorite log file).Better would be to make an
init.d
script that gets started at the appropriate runlevels (you might not want it on runlevel 0, 1 or 6, for instance). That way, you can also have it be restarted automatically if it dies and have a nicer interface to temporarily stopping it.Small examples at http://www.howtoforge.com/forums/archive/index.php/t-3628.html
Big explanation at http://developer.novell.com/wiki/index.php/Writing_Init_Scripts
The & operator will force the process to run in the background, you can run the 'top' command afterwards to see that it is running.
You can run any task in the background on Linux by appending a '&' symbol to the end of the command-line, e.g.
You could use the community version of the Java Service Wrapper available from here:
http://wrapper.tanukisoftware.org/doc/english/download.jsp
You can then have it start at reboot:
http://wrapper.tanukisoftware.org/doc/english/launch-nix-boot-debian.html
Put
&
at the end:java -jar test.jar &