Centos java standalone service - boot on start

5 篇文章 0 订阅

假设我们已经有一个web应用的standalone jar包:hello.jar

在命令行启动:java -jar hello.jar

我们先暂时把hello.jar放在了根目录下


Money see,Monkey do


模仿crond,注意/etc/init.d/hello需要可执行权限



1. /etc/init.d/hello

#!/bin/sh
#
# hello          Start/Stop the hello daemon.
#
# chkconfig: 2345 95 20
# description: hello api server

### BEGIN INIT INFO
# Provides: hello thoughtworks
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start:  2345
# Default-Stop: 95
# Short-Description: run hello api daemon
# Description: hello api server
### END INIT INFO


[ -f /etc/sysconfig/hello ] || {
    [ "$1" = "status" ] && exit 4 || exit 6
}


RETVAL=0
prog="hello"
exec=/usr/sbin/hello
lockfile=/var/lock/subsys/hello
config=/etc/sysconfig/hello


# Source function library.
. /etc/rc.d/init.d/functions


[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog


start() {
    if [ $UID -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    daemon $prog $HELLOARGS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
}


stop() {
    if [ $UID -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
    echo -n $"Stopping $prog: "
    if [ -n "`pidfileofproc $exec`" ]; then
        killproc $exec
        RETVAL=3
    else
        failure $"Stopping $prog"
    fi
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
}


restart() {
    stop
    start
}


reload() {
    echo -n $"Reloading $prog: "
    if [ -n "`pidfileofproc $exec`" ]; then
        killproc $exec -HUP
    else
        failure $"Reloading $prog"
    fi
    retval=$?
    echo
}


force_reload() {
    # new configuration takes effect after restart
    restart
}


rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p /var/run/hello.pid $prog
}


rh_status_q() {
    rh_status >/dev/null 2>&1
}




case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?



2./usr/sbin/hello

/usr/bin/java -jar /hello.jar 1>/dev/null  2>&1 </dev/null &


3./etc/sysconfig/hello

# Settings for the CRON daemon.
# HELLOARGS= :  any extra command-line startup arguments for hello

HELLOARGS=



配置start on boot

/sbin/chkconfig --add hello

/sbin/chkconfig hello on


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值