ubuntu下用jsvc把java程序做为linux服务启动

转自:http://blog.csdn.net/ninjuli/archive/2009/04/28/4131812.aspx

网上关于jsvc跑java的文章就搜到一篇,照着做结果还是出错,所以决定花点时间结合自己最终成功的体会记录下,加深印象。

linux环境:ubuntu8.10server

tomcat:6.0.18

jdk:1.6

1.安装jsvc

在tomcat的bin目录下有一个jsvc.tar.gz的文件,进入tomcat的bin目录下
#tar zxvf jsvc.tar.gz
#cd jsvc-src
#./support/buildconf.sh
#chmod 755 configure
#./configure --with-java=/usr/lib/jvm/java-6-sun (改成你的JDK的位置)
#make

2.编写服务启动类

package com.zhonghui.jsvc.test;

public class TestJsvc {

    public static void main(String args[]) {
        System.out.println("execute main method!");
    }

    public void init() throws Exception {
        System.out.println("execute init method!");
    }

    public void init(String[] args) throws Exception {
        System.out.println("execute init(args) method!");
    }

    public void start() throws Exception {
        System.out.println("execute start method!");
    }

    public void stop() throws Exception {
        System.out.println("execute stop method!");
    }

    public void destroy() throws Exception {
        System.out.println("execute destroy method!");
    }
}

main方法可以去掉,但是init(String[] args),start(),stop(),destroy()方法不能少,服务在启动时会先调用init(String[] args)方法

然后调用start()方法,在服务停止是会首先调用stop()方法,然后调用destroy() 方法.


3.把这个类打包成TestJsvc.jar 放到/home/zhonghui/test目录下(改成你自己的)

4.编写启动服务的脚本 myjsvc

扫描二维码关注公众号,回复: 1394536 查看本文章

#!/bin/sh

# myjsvc This shell script takes care of starting and stopping
#
# chkconfig: - 60 50
# description: jsvc stat is a stat data daemon.
# processname: myjsvc

# Source function library.
#. /etc/init.d   参考了下tomcat脚本没有这句,在这里我注释掉没问题,如你有问题可打开

RETVAL=0
prog="MYJSVC"

# jdk的安装目录
JAVA_HOME=/usr/lib/jvm/java-6-sun
#应用程序的目录
JSVC_HOME=/home/zhonghui/test
#jsvc所在的目录 (找到你自己的jsvc目录)
DAEMON_HOME=/usr/bin 
#用户
MYJSVC_USER=root

# for multi instances adapt those lines.
TMP_DIR=/var/tmp
PID_FILE=/var/run/jsvc.pid

#程序运行是所需的jar包,commons-daemon.jar一定要,后边将会说明会出什么错
CLASSPATH=\
$JAVA_HOME/lib/tools.jar:\
/home/zhonghui/test/TestJsvc.jar:\
/usr/share/java/commons-daemon.jar

case "$1" in
start)
#
# Start jsvc Data Serivce
#
$DAEMON_HOME/jsvc \
-user $MYJSVC_USER \
-home $JAVA_HOME \
-Djava.io.tmpdir=$TMP_DIR \
-wait 10 \
-pidfile $PID_FILE \

###网上的代码由于我懒直接拷贝到脚本里执行,一直提示-outfile not found注释掉继续报下边

###not found,后来发现原来是拷贝过来出了问题,后来删除自己输一遍就没问题,如果你也出现

###这个问题可以从这方面考虑下
-outfile $JSVC_HOME/log/myjsvc.out \
-errfile '&1' \
-cp $CLASSPATH \
com.zhonghui.jsvc.test.TestJsvc
exit $?
;;

stop)
#
# Stop TlStat Data Serivce
#
$DAEMON_HOME/jsvc \
-stop \
-pidfile $PID_FILE \
com.zhonghui.jsvc.test.TestJsvc
exit $?
;;

5. 把myjsvc文件拷贝到/etc/init.d/目录下

6. #chmod -c 777 /etc/init.d/myjsvc


7. 添加服务
默认ubuntu没安装此命令,可apt-get install chkconfig先安装
#chkconfig --add myjsvc
#chkconfig --level 345 myjsvc on

8. 完成,启动服务

#service myjsvc start

你可以从/home/zhonghui/test/log/myjsvc.out文件里看到如下信息:

execute init(args) method

execute start method

#service myjsvc stop

你会发现/home/zhonghui/test/log/myjsvc.out文件里会增加如下信息

execute stop method

execute destroy method

并且在系统重启时会自动启动myjsvc服务

好了,一个简单的 liunx服务就写好了,你可以在TestJsvc的init(),start(),stop(),destroy()方法里添加你的业务,做你想做的事。


错误分析:

Cannot find daemon loader org/apache/commons/daemon/support/DaemonLoader
Service exit with a return value of 1

如果你在log文件里看到这个信息

1.首先看你commons-daemon.jar路径是否正确

2.网上还查到也可能是权限问题,给用户对应权限即可

1. -cp or -classpath is not correct or
2. Permisions on your .jar files are not correct
 for -user.


以上例子均在本机测试通过

猜你喜欢

转载自yedehua.iteye.com/blog/891724