Best Way to Daemonize Java Application on Linux

Best way to daemonize Java application on Linux

While the standard answer to this seems to be jsvc, I have been using djb's daemon tools to be a great way to make anything into a daemon.

I have java, python and a few shell scripts all running as daemons, with a simple way to start/stop them and great logging.

I've used to run daemontools itself as root on initctl as originally designed, but after a few months I decided to run it manually, under a normal user, and using svscan-start for nicer logging.

How to Daemonize a Java Program?

Apache Commons Daemon will run your Java program as Linux daemon or WinNT Service.

How to write a Java daemon


How to write a Java daemon that has 24/7 uptime...

We run a number of 24/365 applications on our Linux servers that just call the Java like the following -- no need for any C wrappers:

nohup java -D... -X... -jar something.jar ... < /dev/null > output.log 2>&1 &

That will put the jar running in the background (nohup ... &) with no input (< /dev/null) and the output (stdout and stderr) redirected to a logfile (> output.log 2>&1). We have distributed logging infrastructure but some console output (such as thread dumps) is still expected. These applications can run for months until we upgrade them.

Can nagios be configured to monitor whether or not my daemon is running, and to alert me or the sys admin when it isn't?

In terms of monitoring, there is much you can do. Nagios looks to have a JMX plugin for testing the information that jconsole displays. There are also a lot of native JMX logging and monitoring utilities out there. We have internal green/yellow/red indicators that can be pulled up using JMX and easily checked. I also have exported a simple JMX/HTTP service from each application to provide status information making it easy for 3rd party monitoring tools to detect failures.

This will be an SMPP client app (or ESME app I guess) which is why I've chosen Java as it seems to be a very mature platform for SMPP.

I assume you mean SMPP? If so then I see no reason why Java couldn't do a good job. Our applications do a wide variety of HTTP, UDP, SMTP, JDBC, LDAP, and other protocols in real time. We use Jgroups at lot which accomplishes a complete authenticated, encrypted, network stack in Java.

What's the best way to manage deployment of new builds? Just stop the daemon and replace the binary as quickly as possible and restart?

In terms of replacing a running binary on the fly, that it more complicated. We have VIPs up front and replace the binaries at our leisure. Our internal protocols are designed to failover. If you do not have a VIP then one thing to consider would be an orderly handoff. You boot the new jar and it talks to the application running the old jar when it is ready to bind to the new port. Then the old application unbinds and the new one binds immediately afterwards. Something like that.

Hope this helps.

Run a Java Application as a Service on Linux

I wrote another simple wrapper here:

#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac

You can follow a full tutorial for init.d here and for systemd (ubuntu 16+) here

If you need the output log replace the 2

nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &

lines for

nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&

Best Method to run a Java Application as a *nix Daemon or Windows Service?

I've had great success with Java Service Wrapper myself. I haven't looked at the others, but the major strengths of ServiceWrapper are:

  • Great x-platform support - I've used it on Windows and Linux, and found it easy on both
  • Solid Documentation - The docs are clear and to the point, with great examples
  • Deep per-platform support - There are some unique features in the window service management system that are supported perfectly by service wrapper (w/o restarting). And on Windows, you will even see your app name in the process list instead of just "java.exe".
  • Standards Compliant - Unlike many ad-hoc Java init scripts, the scripts for service wrapper tend to be compliant with LSB standards. This can end up being very important if you ever want high availability management from something like Linux Heartbeat/HA.

Anyway, just my 2 cents... :)

java background/daemon/service cross platform best practices

You can use the SystemTray classes and install your app as any other in the default platform.

For windows it could be an scheduled task that run at startup.
For Linux and OSX I don't know (besides crontab wich is somehow too technical) but I'm pretty sure they both have a way to do the same thing easily.

Unfortunately (as of today) Apple hasn't finished the 1.6 port.

It won't be a real demon, but an app like Google Desktop.

I've heard Quartz is a good option. But I've never used it.

Java daemon under Linux: Choices?

Some modern Linux distributions have switched to upstart. That's a daeomon starting and stopping all the other services. I'd definitely look into that. Since it solves some tricky problems with production ready start scripts. The downside is that it has no java specific functionality.

Tool for creating a Java daemon service on Linux

Services on Linux are just shell scripts which start background processes. Have a look in /etc/init.d - you can open the files in a text editor. All you need is a bash script which responds to the parameters start and stop in an appropriate way (eg. start will start your service and record the process ID in a known location, stop will kill the process with the PID from the file you created), and then place it in /etc/init.d.

Have a look at Init Scripts and An introduction to services, runlevels, and rc.d scripts



Related Topics



Leave a reply



Submit