14

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!

0

7 Answers 7

9

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.

16

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.

7

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

1
  • 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, 2010 at 5:52
5
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.

1

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 &
1

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

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .