ubuntu下用jsvc把java程序做为linux服务启动
来源:互联网 发布:adobe muse mac 网盘 编辑:程序博客网 时间:2024/06/07 03:01
网上关于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
#!/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.
以上例子均在本机测试通过
- ubuntu下用jsvc把java程序做为linux服务启动
- jsvc 启动java 在linux下的实现原理
- 如何把java程序作为Linux服务启动
- jsvc - 用common-deamon构建java后台服务
- 将java项目做为linux服务
- JSVC编写自己的Linux启动脚本
- 基于JSVC编写Linux启动脚本
- linux下启动服务和监听程序
- 设置Derby做为Linux自启动服务方式运行
- Spring Boot Jar 做为Linux服务启动项
- Linux下启动Java程序
- 启动Java程序为Linux服务
- Oracle做为服务自动启动
- Linux下Java服务启动脚本
- Linux 下启动服务
- Linux下执行Java服务程序
- 通过jsvc将tomcat6添加到linux服务
- 通过jsvc将tomcat6添加到linux服务
- Java 常用 数据转换
- 局域网的IP地址选择
- proteus7.2SP6下载、安装、说明
- 几个字符串转数字的函数
- 游戏.消息管理器
- ubuntu下用jsvc把java程序做为linux服务启动
- javascript 接收页面传值
- 目录间复制文件的线程实现
- 时间管理
- 企业是怎么做大的.
- java连接数据库大全
- 无法安装数据库关系图支持对象
- goto语句的使用
- sql中用到in的一种情况