Java running as a Unix service

29,817

Solution 1

Well, if you want to run your java program even when you exit out of your shell, the following is the most simple way:

$nohup java -jar program.jar &

Solution 2

You need to create an appropriate script in /etc/init.d and link it to /etc/rcX.d directories. The script should support at least start, stop, and status parameters. During start it should run java command with appropriate arguments, probably via nohup java <arguments> &. Then you should save PID of your newly-started process to file /var/run/yourservice.pid. stop command should read this PID file and kill this service. The details vary from distribution to distribution, most distributions provide some macros to make whole job easier. It's best to look at examples of other services in /etc/init.d for your distribution.

Additionally: If your service isn't accessed from other computers from the network, but it opens some port, make it unavailable with firewall.

If your service processes some 'delicate' data, it's good to add another user and invoke an appropriate sudo command in your /etc/init.d file.

Solution 3

You can start it as:

java -jar program.jar

Unix daemons are normally started by init or started by a script in /etc/init.d or /etc/rc.d, and started at specific runlevels - normally by soft links in /etc/rcX.d. (where X is the intended "runlevel" which is normally 3.

I think debian are moving to using "upstart", a init-replacement. It uses config files in /etc/init to define jobs, and they are quite easy to write. Check that out.

Daemons traditionally closes stdin, sdtout and stderr, and does a "double fork" when starting, in order to detach from the session and also to signal that they are ready to handle whatever they should handle. This is not really necessary, as long as the daemon is not started from the terminal.

If you want a simple shell wrapper to start you program; you just need to write a small shell script:

#!/bin/sh
/full/path/to/java -jar /full/path/to/program.jar

... and make it executable (chmod 755 )

Solution 4

This article contains a few useful tricks for running a Java application as a daemon:

http://barelyenough.org/blog/2005/03/java-daemon/

Alternatively, you can have a look at the Apache Commons Daemon project, although this requires native code (Unix and Win32 supported):

http://commons.apache.org/daemon/

Solution 5

You can use a cron job to schedule your program. You can also check out this article for details on how to run scripts on startup. You can write a script that runs your java program and run it on startup as mentioned in the article.

Share:
29,817
Vilius
Author by

Vilius

Updated on July 25, 2022

Comments

  • Vilius
    Vilius almost 2 years

    I have built a little daemon in Java and I would like to run it as a service under Unix (e.g. Debian 5). I have read that there is a possibility of using a Java wrapper, but isn't there any other option which is easier to implement? Can't I just use a Unix command such as xxx java -jar program.jar?

  • barjak
    barjak over 13 years
    On Debian, there is a pre-made script, that you can copy and adapt to your needs : /etc/init.d/skeleton
  • KarlP
    KarlP over 13 years
    This is good if you want to run a program periodically. The program should then terminate when its done.
  • JeremyP
    JeremyP over 13 years
    consider redirecting stderr and stdout as part of the command. e.g. nohup java .... 1>/dev/null 2>&1 &
  • Raedwald
    Raedwald over 10 years
    On Linux computers there is a utility command, chkconfig, which creates the links in the /etc/rcX directories for you by examining special format comments in your script.