1

I'm trying to use JSVC to run a java program as a daemon. This is the simple code that I have used:

package daemonexample;
public class DaemonExample implements Daemon {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
}

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

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

@Override
public void stop() throws Exception {
    System.out.println("stopping ...");
}

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

}

And the command: jsvc -debug -home $JAVA_HOME -cp /path/to/commons-daemon.jar:/path/to/DaemonExample.jar -user coder -outfile /tmp/example.out -errfile /tmp/example.err -pidfile /tmp/example.pid daemonexample.DaemonExample

When I run this, I get: "redirecting stdout to /tmp/example.out and stderr to /tmp/example.err". But the target files are empty. What am I missing here?

coder
  • 1,901
  • 5
  • 29
  • 44
  • 6
    The one and only feature that I'm aware Jsvc brings with it is the ability for a JVM to start as root, bind to privileged ports, and then switch to a non-privileged user. Nothing in it really makes it easier to run a Java app as a daemon. You can just as easily `nohup java -jar foo.jar &` or whatever. You might just be buying yourself increased complexity for no benefit. – Ryan Stewart Jan 01 '13 at 05:06

2 Answers2

0

checkout your /tmp/example.err. it should contain error details.

Following may be the problems:

1: you forgot import org.apache.commons.daemon.*; ?
2: if yes, then DaemonExample.class is not getting created.
3: make sure DaemonExample.class exists in DaemonExample.jar
4: jsvc, usually must be called using full path. i.e. /usr/bin/jsvc .... check your debug info
Icarus3
  • 2,310
  • 14
  • 22
  • 1: importing commons daemon is done. 2: Also the class is there in jar. If I add a main function and run the jar directly from cmd line, it works fine. So that shows the class exists in jar. 4: I'm also calling jsvc with complete path And example.err is also empty – coder Jan 02 '13 at 13:42
  • What is the output of jsvc -debug ? Could you post it here ? – Icarus3 Jan 02 '13 at 13:49
  • @lcarus3 it was a huge output. So had to just post it as part of answer – coder Jan 02 '13 at 16:52
-2

DUMPING PARSED COMMAND LINE ARGUMENTS | Detach: True | Show Version: No | Show Help: No | Check Only: Disabled | Stop: False | Wait: 0 | Run as service: No | Install service: No | Remove service: No | JVM Name: "null" | Java Home: "/Library/Java/Home" | PID File: "/tmp/example.pid" | User Name: "root" | Extra Options: 1 | "-Djava.class.path=/Users/username/NetBeansProjects/DaemonExample/dist/DaemonExample.jar" | Class Invoked: "daemonexample.DaemonExample" | Class Arguments: 0

No need to change user to 'root'! User 'root' validated Home not specified on command line, using environment Home not on command line or in environment, searching Attempting to locate Java Home in /System/Library/Frameworks/JavaVM.framework/Home Attempting to locate VM configuration file /System/Library/Frameworks/JavaVM.framework/Home/jre/lib/jvm.cfg Attempting to locate VM configuration file /System/Library/Frameworks/JavaVM.framework/Home/lib/jvm.cfg Found VM configuration file at /System/Library/Frameworks/JavaVM.framework/Home/lib/jvm.cfg Found VM client definition in configuration Checking library /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libclient.dylib Found VM jvm definition in configuration Checking library /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libjvm.dylib Found VM hotspot definition in configuration Checking library /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libhotspot.dylib Found VM server definition in configuration Checking library /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libserver.dylib Found VM classic definition in configuration Checking library /System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libclassic.dylib Cannot locate library for VM classic (skipping) Java Home located in /System/Library/Frameworks/JavaVM.framework/Home DUMPING JAVA HOME STRUCTURE | Java Home: "/System/Library/Frameworks/JavaVM.framework/Home" | Java VM Config.: "/System/Library/Frameworks/JavaVM.framework/Home/lib/jvm.cfg" | Found JVMs: 4 | JVM Name: "client" | "/System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libclient.dylib" | JVM Name: "jvm" | "/System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libjvm.dylib" | JVM Name: "hotspot" | "/System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libhotspot.dylib" | JVM Name: "server" | "/System/Library/Frameworks/JavaVM.framework/Home/../Libraries/libserver.dylib"

redirecting stdout to /tmp/example.out and stderr to /tmp/example.err

coder
  • 1,901
  • 5
  • 29
  • 44