免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4875 | 回复: 5
打印 上一主题 下一主题

为什么脚本中后台运行java程序,java程序启动之后总是自动终止! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-09 17:39 |只看该作者 |倒序浏览
我写了脚本放到/etc/init.d下面做成服务,start服务总是启动之后就终止.

具体情况是这样的,写了脚本test放到/etc/init.d下面,里面提供start,stop等功能.
start是执行java程序,
1、用java -cp test.jar com.test.test可以执行,但是会占用终端。
2、用service test start运行,java程序确实运行了,但是之后马上终止了.
service test stop等命令执行却没有问题。,.
脚本如下
#!/bin/bash
#
# chkconfig: 235 99 99
# description: Starts and stops test service

prog="test"
version="$prog Ver 2006.04.04 Distrib 1.1.2, for linux"
prog_path="/usr/local/test"

start() {
pre=$(ps -ax|grep java|grep "test.jar"|grep -v grep)
if [ -n "$pre" ]
then
        echo -e "$prog is already running!"
        exit 0
fi

if cd $prog_path
then
stty tostop
/usr/local/jdk/bin/java -cp /usr/local/test/test.jar com.test.test &
fi
echo -n "Starting $prog "
psstart=$(ps -ax|grep java|grep "test.jar"|grep -v grep)
if [ -n "$psstart" ]
then
        echo -e "        [ OK ]"
else        
        echo -e "        [ FAILED ]"
fi
}

status() {
test=$(ps -ax|grep java|grep "test.jar"|grep -v grep)
echo -n "$prog "
if [ -n "$test" ]
then
        echo -e "is running."
else
        echo -e "is stopped."
fi
}

stop() {
befpid=$(ps -ax|grep java|grep "test.jar"|grep -v grep)
if [ -n "$befpid" ]
then
pid=$(ps -ax|grep java|grep "test.jar"|grep -v grep|cut -d" " -f1)
if [ -z "$pid" ]
then
pid=$(ps -ax|grep java|grep "test.jar"|grep -v grep|cut -d" " -f2)
fi
stty -echo
kill -9 "$pid"
stty echo
fi
sleep 1
psstop=$(ps -ax|grep java|grep "test.jar"|grep -v grep)
echo -n "Shut down $prog "
if [ -n "$psstop" ]
then
        echo -e "        [ FAILED ]"
else        
        echo -e "        [ OK ]"
fi
}

version() {
echo "$version"
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        stop
        start
        ;;
  status)
        status
        ;;
  version)
        version
        ;;
  *)
        echo -e $"$version \nUsage: $0 {start|stop|restart|status|version}"
        exit 1
esac

[ 本帖最后由 placidy 于 2006-5-10 09:19 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-05-09 18:11 |只看该作者
大家都下班了?

论坛徽章:
0
3 [报告]
发表于 2006-05-10 08:50 |只看该作者
是我哪里没有说明白么?

论坛徽章:
0
4 [报告]
发表于 2006-05-10 09:09 |只看该作者
整理一下我的问题,/etc/init.d/test 脚本中试过以下几种:
1、java -cp test.jar com.test.test
可以运行,但是占用终端
2、java -cp test.jar com.test.test &
运行之后自动终止
3、nohup java -cp test.jar com.test.test
可以运行,但占用终端
4、nohup java -cp test.jar com.test.test &
运行之后自动终止

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
5 [报告]
发表于 2006-05-10 10:28 |只看该作者
说明 你的test.test需要终端,不能放在后面运行,改test.test

论坛徽章:
0
6 [报告]
发表于 2006-05-12 10:26 |只看该作者
恩,我取消了终端交互,可以了.谢谢waker.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP