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:
Issue the kill -15 command with the process identifier number to stop the daemons. For AIX® and Linux x86_64 GPFS™ file systems, issue the command dmkilld to stop the recall daemons. Verify that the daemons are no longer running.
Jsvc is a set of libraries and applications that facilitates running Java applications on Linux, UNIX, and similar operating systems. Using Jsvc with Red Hat JBoss Web Server 3 allows Tomcat to switch identities. Using Jsvc, Tomcat can perform root-level operations and then revert to a non-privileged user.
Jsvc is a daemon process so it should be started as root and the -user parameter allows to downgrade to an unprivilegded user. When the -wait parameter is used, the launcher process waits until the controller says "I am ready", otherwise it returns after creating the controller process.
There are two ways to use Commons Daemon: by implementing the daemon interface or by calling a class that provides the required methods for daemon. For example, Tomcat-4.1. x uses the daemon interface and Tomcat-5.0. x provides a class whose methods are called by JSVC directly.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With