36

I wrote a program and now I am expected to convert it to a daemon. Since I am a noob and dont know anything about it, can you please tell me how can I do it or can you please give me basic tutorials or readings about it ? I really want to learn how I can do it ?

Thank you all

Ozer
  • 1,245
  • 4
  • 20
  • 27
  • 1
    There is setDaemon() in Thread Class. Are u looking for that? If you check the javadoc for this method, you can understand how to do it. – Naveen Babu Oct 07 '11 at 13:50

3 Answers3

70

Java class:

package example;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.daemon.*;

class EchoTask extends TimerTask {
    @Override
    public void run() {
        System.out.println(new Date() + " running ...");
    }
}

public class Main implements Daemon {

    private static Timer timer = null;

    public static void main(String[] args) {
        timer = new Timer();
        timer.schedule(new EchoTask(), 0, 1000);
    }

    @Override
    public void init(DaemonContext dc) throws DaemonInitException, Exception {
        System.out.println("initializing ...");
    }

    @Override
    public void start() throws Exception {
        System.out.println("starting ...");
        main(null);
    }

    @Override
    public void stop() throws Exception {
        System.out.println("stopping ...");
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public void destroy() {
        System.out.println("done.");
    }

 }

Shell start/stop script:

#!/bin/sh

# Setup variables
EXEC=/usr/bin/jsvc
JAVA_HOME=/usr/lib/jvm/java-6-sun
CLASS_PATH="/usr/share/java/commons-daemon.jar":"/path/to/your.jar"
CLASS=example.Main
USER=foo
PID=/tmp/example.pid
LOG_OUT=/tmp/example.out
LOG_ERR=/tmp/example.err

do_exec()
{
    $EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS
}

case "$1" in
    start)
        do_exec
            ;;
    stop)
        do_exec "-stop"
            ;;
    restart)
        if [ -f "$PID" ]; then
            do_exec "-stop"
            do_exec
        else
            echo "service not running, will do nothing"
            exit 1
        fi
            ;;
    *)
            echo "usage: daemon {start|stop|restart}" >&2
            exit 3
            ;;
esac

And the effect:

$ ./service start && sleep 5 && ./service stop
$ cat /tmp/example.out
initializing ...
starting ...
Fri Oct 07 16:26:54 EEST 2011 running ...
Fri Oct 07 16:26:55 EEST 2011 running ...
Fri Oct 07 16:26:56 EEST 2011 running ...
Fri Oct 07 16:26:57 EEST 2011 running ...
Fri Oct 07 16:26:58 EEST 2011 running ...
stopping ...
done.
barti_ddu
  • 10,179
  • 1
  • 45
  • 53
  • Nice approach, but I have a one question in mind. How do you define classpath if there are more than one dependent jar files? Do I need define one by one separately as per your script? – Switch Mar 18 '13 at 03:26
  • @MMRUser: if the dependent jars are specified in `your.jar` manifest, you don't. – barti_ddu Mar 18 '13 at 08:28
4

Are you 100% sure you need to use jsvc? If you just want to have your application bind to a port and run at boot time, you don't need to use it. jsvc allows your application to bind to a privileged port (<1024) and then resume running as a normal user.

lynks
  • 5,599
  • 6
  • 23
  • 42
  • Actually I am sure because I am expected to do so. But I really dont know how to do that – Ozer Oct 07 '11 at 12:47
  • 2
    jsvc also gives you things like startup error handling, PID tracking, and clean shutdown. It can be tricky to get a Java-based app running as a Linux service without those features. – Ethan T Apr 13 '16 at 22:33
3

Check out the Jakarta Commons Daemon documentation. There's an example of how to implement the Daemon interface on their wiki. Once you've implemented Daemon, you should be able to start the process by following the "Starting jsvc" and "Using jsvc" docs.

jtoberon
  • 8,706
  • 1
  • 35
  • 48