- 论坛徽章:
- 0
|
我写了脚本放到/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 编辑 ] |
|