I'm building a standalone application to run as a Linux (Ubuntu) daemon service, using Java. For that, I'm using the Java Service Wrapper lib.

During the execution of the service I'd like to be able to query for some status of the tasks executed by the application. In other words, I'd like to be able to print a custom message when I type on terminal:

service my-app status

Currently the message it prints is something like this:

My App is running: PID:1000, Wrapper:STARTED, Java:STARTED

I understand I need to use a more complex integration method (rather than the simplest one), but I couldn't find how to intercept the "status" call to print my own message.

How can I customize that message, using the Java Service Wrapper?

有帮助吗?

解决方案

I got a half-solution. Inspecting the wrapper script code, as suggested by @Naytzyrhc, I found that the wrapper lib reads 3 files to create the status message:

  1. bin/my-app.pid to print the PID of the running process;
  2. bin/my-app.status to print the status of the wrapper itself;
  3. bin/my-app.java.status to print the status of the wrapped application.

So, in the application code, to override the status message, just write the message in the my-app.java.status file.

There's only one gotcha: if the status message contains line breaks, the service my-app status doesn't print them, because it uses the echo command (as stated in this question Capturing multiple line output into a Bash variable). To solve this problem, just change the line from:

eval echo `gettext '$APP_LONG_NAME is running: PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS'`

to:

eval echo `gettext '$APP_LONG_NAME is running: PID:$pid, Wrapper:$STATUS, Java:"$JAVASTATUS"'`

(Using double quotes on $JAVASTATUS).

This is a half-solution because it doesn't fire an event to the running application, as I wanted. But it works for customizing the status message: it depends on the application how often the message is updated.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top