Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

migrated from stackoverflow.com Jun 18 '10 at 5:22

This question came from our site for professional and enthusiast programmers.

7 Answers 7

up vote 6 down vote accepted

several ways:

  1. appending & at the back. However, using this, the program will still be terminated if you closed the terminal that started the program.

  2. 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.

  3. 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.

share|improve this answer

A command line prompt I have always used for long run times to last through logoffs is "nohup" so in your case is

nohup java -jar test.jar &

The & is important so you can get another shell running. I believe this will not last through reboots.

share|improve this answer

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

share|improve this answer
    
another possibility, if you want it to run with just user perms is to put it in cron and use '@reboot' as the time to run. See 'man 5 crontab' for details. –  pjz Jun 18 '10 at 5:52
java -jar test.jar &

The & operator will force the process to run in the background, you can run the 'top' command afterwards to see that it is running.

share|improve this answer

You can run any task in the background on Linux by appending a '&' symbol to the end of the command-line, e.g.

java -jar test.jar &
share|improve this answer

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

share|improve this answer

Put & at the end: java -jar test.jar &

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.