假设我们已经有一个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