[英]How to convert a java program to daemon with jsvc?
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 谢谢你们
Java class: Java类:
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: Shell启动/停止脚本:
#!/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.
Are you 100% sure you need to use jsvc? 您是否100%确定需要使用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.
jsvc允许您的应用程序绑定到特权端口(<1024),然后以普通用户身份继续运行。
Check out the Jakarta Commons Daemon documentation. 查看Jakarta Commons Daemon文档。 There's an example of how to implement the
Daemon
interface on their wiki . 有一个如何在他们的wiki上实现
Daemon
接口的例子。 Once you've implemented Daemon
, you should be able to start the process by following the "Starting jsvc" and "Using jsvc" docs . 一旦您实现了
Daemon
,您应该能够通过“启动jsvc”和“使用jsvc”文档来启动该过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.