Apache jsvc fails to stop daemon

Noah picture Noah · Aug 23, 2012 · Viewed 8k times · Source

I am using a native compiled jsvc to launch a java daemon. I am running this on an openSUSE 32 bit vm. The code implements Apache's daemon interface, and I'm executing the deamon with the following command.

./jsvc -home jre -errfile logs/jsvc.err -pidfile daemon.pid -cp <my_classpath> com.loader.loaderservice.LoaderDaemon

It starts and runs without a problem. I can start the daemon as a normal user and as root. However, when I go to terminate the daemon, jsvc kills the process rather than issuing a stop command.

./jsvc -stop -home jre -outfile logs/jsvc.err -errfile logs/jsvc.err -pidfile daemon.pid -cp <my_classpath> com.loader.loaderservice.LoaderDaemon

The daemon process dies, but does not execute any of its shutdown steps (for example, it should log, mark a record in the db, etc). I get the following in the logs/jsvc.err file, and it doesn't write any other logs:

Service exit with return value 143

After googling the error, I'm seeing a handful of people who all have seen the same thing, but nowhere can I find a good resolution (http://mail-archives.apache.org/mod_mbox/commons-dev/200401.mbox/%[email protected]%3E, http://www.tek-tips.com/viewthread.cfm?qid=1014679, http://threebit.net/mail-archive/tomcat-users/msg03832.html).

UPDATE: Using Apache's window's service launcher (procrun) I am able to start and stop the service without any problems. The problem appears to be only jsvc related, and only on stopping the daemon.

UPDATE 2: After reading the http://commons.apache.org/daemon/jsvc.html#Starting_jsvc more carefully, I noticed that the stop tag I'm using issues a kill command pn the process via the pid file I specify. It seems that jsvc doesn't actually stop the daemon gracefully by design. This is consistent with the behavior I'm seeing, as the very verbose stop method isn't writing any messages out.

-stop        stop the service using the file given in the -pidfile option

New Questions:

  • If my java main has implemented the Apache Daemon interface, how do I issue a 'stop' on the running daemon?
  • Do I need something other than jsvc (which seems like it's only useful for starting or killing the daemon)?

Answer

Noah picture Noah · Aug 29, 2012

Ah, ok. It turns out that the jsvc stop command was behaving correctly. I had to dig into the way that processes receive termination messages in Linux/Unix via the kill command. Jsvc issues a kill -15 (which is a soft kill) on the daemon. See: http://commons.apache.org/daemon/ and http://en.wikipedia.org/wiki/Kill_(command) for the description of how unix processes receive messages.

The real issue was in the construction of the daemon. In my start method, the daemon looped until a shutdown command was issued, which prevented the daemon from giving up control as the daemon child process.

I had this:

@Override
public void start() 
{
    doStartWork();
    while (isAlive()) 
    {
        Thread.sleep(1000); 
    }
}

I should have had below, so I could return and allow the daemon thread to receive signals from the OS. See http://commons.apache.org/daemon/jsvc.html#How_jsvc_works, sepcifically the section under: 'Controlled process:'

@Override
public void start() 
{
    doStartWork();
}